题目连接: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. 瞎j8封装第二版之数据层的封装

    看了以前写的代码,对就是下面这个 手把手封装数据层之DataUtil数据库操作的封装 觉得以前写的代码好烂啊!!!,重新理了一下思路,写得更规范和简练,应该效率也会高很多,用了一下下午写的连接池(半废 ...

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

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

  3. C 函数参数 char **s与char *s[]

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/126 先来看一个小例子 : 编写函数遍历一个整型数组的元素,数组 ...

  4. Docker(九):Docker容器卷插件

    1.Convoy 1.1 安装 [root@MediaServer tmp]# tar xvf convoy.tar.gz convoy/ convoy/convoy-pdata_tools conv ...

  5. CSS3 使用选择器在页面插入内容

    使用选择器来插入文字 h2:before{ content:'COLUMN'; color:white: background-color:orange: padding:1px 5px; } 注意点 ...

  6. 手撕vue-cli配置文件——config篇

    最近一直在研究webpack,突然想看看vue-cli中的webpack是如何配置,查阅了很多相关的文章,所以也想出几篇关于vue-cli配置的东西.正所谓"工欲善其事必先利其器" ...

  7. Linux(CentOS6.5)修改系统市区被中国标准时间(北京时间)

    本文地址http://comexchan.cnblogs.com/ ,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 备份时区配置文件 cp /etc/localtime /etc/l ...

  8. WebDriver API 大全

    访问某网页地址:driver.get(url)  或  driver.navigate().to(url) 访问上一个访问的网页(模拟单击浏览器的后退按钮)driver.navigate().back ...

  9. 伽罗瓦域(有限域)GFq^12上元素的1→2→4→12塔式扩张(1)------第一次扩张

    伽罗瓦域是抽象代数下的域论分支中的内容,这部分想必很多人都比较熟悉,此处不再赘述. 最近,国密算法中的SM2和SM9已经成为国际标准,其中SM9算法在椭圆曲线离散对数难题的基础上,添加了若干个双线性配 ...

  10. 1.1 About Percona XtraDB Cluster

    摘要: 出处:kelvin19840813 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎 ...