题目连接: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. 使用VMware安装linux虚拟机以及相关配置

    前言 使用VMware安装虚拟机这个一般都知道,操作简单.而本文主要讲使用虚拟机的后续相关配置.并记录使用过程中遇到的问题以及一些技巧.本篇文章以后回持续更新的... 安装包准备 VM:12 Linu ...

  2. TxDragon的训练5

    Solution 代码:由乃: //MADE BY QT666 #include<iostream> #include<cstdio> #include<algorith ...

  3. 如何在yarn上运行Hello World(一)

    1.YARN是什么 YARN  (Yet Another Resource Negotiator,另一种资源协调者) 是hadoop上的一种资源调度器,它是一个通用资源管理系统,可以为上层应用提供统一 ...

  4. MVC 框架

    MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码 ...

  5. python+selenium自动化测试_1

    前言 回顾一下python+selenium基础,并整理相关知识点,分享给有需要,在前进道路上的朋友. print打印 #打印Hello World print("Hello World&q ...

  6. Java自己动手写连接池四

    Java自己动手写连接池四 测试: package com.kama.cn; import java.sql.Connection; public class Test { public static ...

  7. python Is 与== 的坑

    以前看过一篇python技术贴,说用is替代==,这样更加pythonic?然后我就能把用'=='的地方用'Is'替代,结果程序运行结果的偏差很大,甚至完全不同.后来发现,Is与==使用上是有区别的. ...

  8. 基于Vue+node.js的个人博客

    前言 作为一个年轻的程序员,而且是作为一个未来的前端工程师,怎么能没有一个属于自己的博客呢,于是乎在暑假咸鱼了一个多月后开始了我的博客的编写. 技术栈 前端 vue.js+scss  因为当时没学vu ...

  9. robotframework的学习笔记(十二)------DatabaseLibrary 库

    1.安装DatabaseLibrary库 DatabaseLibrary 下载地址:https://pypi.python.org/pypi/robotframework-databaselibrar ...

  10. 面试经验And总结

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