POJ 1775 Sum of Factorials (ZOJ 2358)
http://poj.org/problem?id=1775
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1334
题目大意:
给一个数n看看n是否能够拆成几个阶乘的和
如9=1!+2!+3!
方法一:
最初想法是直接打出0~10的阶乘,10的阶乘已经大于n的范围
然后DFS,也过了。不过时间好惨。。。
注意n=0输出NO和0!=1
#include<cstdio>
#include<cstring>
const int MAXN=10;
int temp[12]={1,1};
bool used[12];
bool ok;
void dfs(int cur,int sum,int target)
{
if(sum >target)
return; if(sum==target)
{
ok=true;
return ;
} for(int i=cur;i<=MAXN;i++)
{
if(used[i]==false)
{
used[i]=true;
dfs(i,sum+temp[i],target);
used[i]=false;
}
}
}
int main()
{ for(int i=2;i<=MAXN;i++)
temp[i]=temp[i-1]*i; int n;
while(scanf("%d",&n),n>=0)
{ if(n==0)
{
printf("NO\n");
continue;
}
memset(used,0,sizeof(used));
ok=false;
dfs(0,0,n); if(ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
方法二:
贪心。。
然后把不超过n的阶乘减去。
因为n! >= sum(1 ! + 2!+……n-1!)
#include<cstdio>
const int MAXN=10;
int main()
{
int temp[12]={1,1};
for(int i=2;i<=MAXN;i++)
temp[i]=temp[i-1]*i; int n;
while(scanf("%d",&n),n>=0)
{
if(n==0)
{
printf("NO\n");
continue;
}
for(int i=MAXN;i>=0;i--)
{
if(n >=temp[i])
n-=temp[i];
}
if(n==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
POJ 1775 Sum of Factorials (ZOJ 2358)的更多相关文章
- zoj 2358,poj 1775 Sum of Factorials(数学题)
题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...
- POJ 1775 Sum of Factorials 数论,基础题
输入一个小于1000000的正整数,是否能表达成式子:a1!+a2!+a3!+...+an (a1~an互不相等). 因为10!>1000000,所以先打1~10的阶乘表.从a[10]开始递减判 ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- POJ 3090 Visible Lattice Points (ZOJ 2777)
http://poj.org/problem?id=3090 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1777 题目大意: ...
- POJ 1707 Sum of powers(伯努利数)
题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)
POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径) Description Freddy Frog is sitting on ...
随机推荐
- [置顶]
Docker学习总结(3)——Docker实战之入门以及Dockerfile(三)
应用镜像 csphere/wordpress:4.2 # cd docker-training/wordpress/ # ls -a . license.txt wp-config-sample.ph ...
- springboot整合Beetl、BeetlSql实现ajax分页
Beetl是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,它功能强大,性能良好,超过当前流行的模板引擎.而且还易学易用. BeetSql是一个 ...
- GCC中-fpic解惑(转载)
参考: 1.<3.18 Options for Code Generation Conventions>2.<Options for Linking>3.<GCC -fP ...
- 安装 VNC 和 XRDP
安装 VNC 和 XRDPapt-get install vnc4server tightvncserver xrdp fluxbox xtermcat >vncstop.sh << ...
- 点击事件-click,longclick
今天在修改一个问题的时候,遇到了click,longclick事件触发情况.记录下来. 代码 tView.setOnLongClickListener(new OnLongClickListener( ...
- 用VXE保护Linux系统安全
本文被转载在:http://www.linuxso.com/a/linuxxitongguanli/161.html 650) this.width=650;" onclick=&quo ...
- 界面实例--旋转的3d立方体
好吧,这里直接编辑源码并不允许设置css和js……毕竟会有危险……那直接放代码吧 <!DOCTYPE html> <html> <head> <meta ch ...
- NuGet 使用及dll管理
NuGet学习笔记(1)——初识NuGet及快速安装使用 作者: 懒惰的肥兔 来源: 博客园 发布时间: 2012-05-20 21:33 阅读: 53168 次 推荐: 33 原文链接 ...
- mysql新加入用户与删除用户详细操作命令
方法1 :使用mysql root(root权限)用户登陆直接赋权也能够创建用户 /usr/bin/mysqladmin -u root password 123456 mysql -uroot -p ...
- 指示函数(indicator function) 的作用
1. counter 指示函数常用于次数(满足某一断言或条件)的统计: 2. 二维的离散指示函数 ⇒ assignment solution xij∈{0,1},∑jxij=1 ∑jxij=1:行和为 ...