题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2212

DFS

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6322    Accepted Submission(s): 3898

Problem Description
A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer.

For example ,consider the positive integer 145 = 1!+4!+5!, so it's a DFS number.

Now you should find out all the DFS numbers in the range of int( [1, 2147483647] ).

There
is no input for this problem. Output all the DFS numbers in increasing
order. The first 2 lines of the output are shown below.

 
Input
no input
 
Output
Output all the DFS number in increasing order.
 
Sample Output
1
2
......
 
Author
zjt
题解:开始想到10位数,枚举每一位数字肯定会超时,O(10的10次方) 根据枚举的情况,1!+4!+5!和5!+4!+1!是一样的,所以可以从大到小按照顺序枚举出所有情况,即可,这样下一个位置枚举的时候就不可以出现比最后小的元素了,这里注意0的阶乘是1,而且为了避免第一位出现0 的情况,从最后一位向前枚举,最后的输出结果只有4个数,所以可以之间打表输出这4个数即可
 #include<cstdio>
#include<algorithm>
using namespace std;
//#define N 8776444321ll//要在后面加ll 这样不会超int
#define ll long long
const ll N = ;
ll ans[];
int c ;
int f[],g[];
int a[];
int ck(ll sum)
{
for(int i = ; i < ; i++) f[i] = ,g[i] = ;
ll v = sum;
ll tm = ;
while(v)
{
tm+=a[v%];
f[v%]++;
v/=;
}
v = tm;
while(tm)
{
g[tm%]++;
tm/=;
}
for(int i = ;i < ; i++) if( f[i]!=g[i] ) return -;
return v;
}
void dfs(int cur, ll sum)
{
ll res = ck(sum);
if(sum>N) return ;
if(res!=-) ans[c++] = res;
for(int i = cur ; i >= ; i-- )
{
if(sum==&&i==) continue;
dfs(i,sum*+i);
}
}
int main()
{
a[] = a[] = ;
for(int i = ; i < ; i++)
a[i] = a[i-]*i;
c = ;
dfs(,);
sort(ans,ans+c);
for(int i = ; i < c ; i++)
printf("%lld\n",ans[i]);
return ;
}

其实得到输出结果后可以直接打表:

 #include <cstdio>
using namespace std;
int main()
{
puts("");
puts("");
puts("");
puts("");
return ;
}

DFS(dfs)的更多相关文章

  1. PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides ...

  2. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  3. UVA 291 The House Of Santa Claus (DFS求一笔画)

    题意:从左下方1开始,一笔画出圣诞老人的屋子(不过话说,圣诞老人的屋子是这样的吗?这算是个屋子么),输出所有可以的路径. 思路:贴代码. #include <iostream> #incl ...

  4. Shredding Company(dfs)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3519   Accepted: 2009 Description You h ...

  5. 素数回文(dfs,有bug)

    素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. [Swust OJ 799]--Superprime Rib(DFS)

    题目链接:http://acm.swust.edu.cn/problem/799/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...

  7. Lake Counting (DFS)

    N*M的园子,雨后积起了水.八连通的积水背认为是连接在一起的.请求出园子里总共有多少水洼? dfs(Depth-First  Search)  八个方向的简单搜索.... 深度优先搜索从最开始的状态出 ...

  8. BFS和DFS详解

    BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...

  9. 训练[2]-DFS

    题目A: 题目B[https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...

随机推荐

  1. Java I/O---Reader & Writer(字符流)

    1.Reader & Writer 当我们初次看见Reader和Writer类时,可能会以为这是两个用来替代InputStream和OutputStreamt的类,但实际上并非如此. 尽管一些 ...

  2. C# 内存模型

    C# 内存模型 This is the first of a two-part series that will tell the long story of the C# memory model. ...

  3. BZOJ 4816 数字表格

    首先是惯例的吐槽.SDOI题目名称是一个循环,题目内容也是一个循环,基本上过几年就把之前的题目换成另一个名字出出来,喜大普奔亦可赛艇.学长说考SDOI可以考出联赛分数,%%%. 下面放解题报告.并不喜 ...

  4. su 和 sudo 命令的区别-转载

    link 一. 使用 su 命令临时切换用户身份                   1.su 的适用条件和威力   su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要 ...

  5. 2.Nginx日常维护技巧

    Nginx日常维护技巧 Nginx配置正确性检查 nginx提供了配置文件调试功能,可以快速定义配置文件存在的问题.执行如下命令检测配置文件的正确性: [root@localhost 桌面]# whi ...

  6. cleanMyMac

    想看外国网站可以找我,facebook.youtube.XX大片等,只要8元钱,无限制用到服务器关闭.看大片流畅不成问题 需要cleanMyMac的请加微信只要10或直接拍 http://a.p6ff ...

  7. 最优化算法:BFGS算法全称和L-BFGS算法全称

    在最优化算法研究中按时间先后顺序出现了许多算法包括如下几种,这里介绍下他们的全称和英文名称: 1.最速下降法(Gradient descent) 2.牛顿法(Newton method) 3. 共轭梯 ...

  8. Django学习日记05_模板_模板语言

    Variables 在模板中,使用两个大括号包含变量的方式来使用变量: {{ name }} 该变量应该作为键值对中的键,在Context中能被查找到. Tags 模板中使用Tags来进行简单的逻辑: ...

  9. VIM常见用法总结

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  10. 面试经验And总结

    作为一个实习生,我大二即将读完,因为自己是大专的和本科没法比,没有他们的 知识基础,没有他们的充裕的时间,没有那个本科毕业证,没有学位证书.作为一个大专生我在找工作的时候总是充满了自卑,可自己有事那种 ...