九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1845
解决:780
- 题目描述:
-
John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics, meteorology, science, computers, and game theory. He was noted for a
phenomenal memory and the speed with which he absorbed ideas and solved problems. In 1925 he received a B.S. diploma in chemical engineering from Zurich Institute and in 1926 a Ph.D. in mathematics from the University of Budapest, His Ph.D. dissertation on
set theory was an important contributions to the subject.
At the age of 20, von Neumann proposed a new definition of ordinal numbers that was universally adopted. While still in his twenties, he made many contributions in both pure and applied mathematics that established him as a mathematician of unusual depth.
His Mathematical Foundation of Quantum Mechanics (1932) built a solid framework for the new scientific discipline.
During this time he also proved the mini-max theorem of GAME THEORY. He gradually expanded his work in game theory, and with coauthor Oskar Morgenstern he wrote Theory of Games and Economic Behavior (1944).
There are some numbers which can be expressed by the sum of factorials. For example 9, 9 = 1! + 2! + 3! . Dr. von Neumann was very interested in such numbers. So, he gives you a number n, and wants you to tell whether or not the number can be expressed
by the sum of some factorials.
Well, it is just a piece of case. For a given n, you will check if there are some xi, and let n equal to Σt (上标) i=1(下标) xi! (t≥1, xi≥0, xi = xj <==> i = j)
t
即 Σ xi! (t≥1, xi≥0, xi = xj <==> i = j)
i=1
If the answer is yes, say "YES"; otherwise, print out "NO".
- 输入:
-
You will get a non-negative integer n (n≤1,000,000) from input file.
- 输出:
-
For the n in the input file, you should print exactly one word ("YES" or "NO") in a single line. No extra spaces are allowed.
- 样例输入:
-
9
2
- 样例输出:
-
YES
YES
思路:
题意是问给出的数是否能表示成不同阶乘的和。
由于给出的n范围较小,最大的阶乘肯定不会超过10,因此尝试从10!开始逐步到1!看是否属于n的分解式即可。
代码:
#include <stdio.h> int main(void)
{
int n, i;
int fac[10]; while (scanf("%d", &n) != EOF)
{
if (n == 0)
{
printf("NO\n");
continue;
}
fac[0] = 1;
for (i=1; i<10; i++)
fac[i] = fac[i-1]*i;
for (i=9; i>=0; i--)
{
if (n >= fac[i])
n -= fac[i];
if (n == 0)
break;
}
if (i == -1)
printf("NO\n");
else
printf("YES\n");
} return 0;
}
/**************************************************************
Problem: 1038
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/
九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)的更多相关文章
- 【九度OJ】题目1179:阶乘 解题报告
[九度OJ]题目1179:阶乘 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1179 题目描述: 输入n, 求y1=1!+3!+-m ...
- 九度OJ 1076:N的阶乘 (数字特性、大数运算)
时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:6384 解决:2238 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...
- 九度OJ 1067:n的阶乘 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6476 解决:2471 题目描述: 输入一个整数n,输出n的阶乘 输入: 一个整数n(1<=n<=20) 输出: n的阶乘 样例 ...
- 九度OJ 1205 N阶楼梯上楼问题 (DP)
题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2817 解决:1073 题目描写叙述: N阶楼梯上楼问题:一次能够走两阶或一阶.问有多少种上楼方式. (要 ...
- 九度OJ 1345:XXX定律之画X (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:361 解决:157 题目描述: 给你一个n,然后让你输出F(n) 规则是这样的,F(n)的输出结果是: F(n-1) F(n-1) ...
- 九度OJ 1260:珍珠项链 (字符串处理、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:101 解决:27 题目描述: 假设有一条珍珠项链,有很多珍珠,r代表红色, b代表蓝色, w代表白色. 假设你在某一处剪开之后,你会沿着顺时 ...
- 九度OJ 1161:Repeater(复制器) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1449 解决:508 题目描述: Harmony is indispensible in our daily life and no one ...
- 九度OJ 1040:Prime Number(质数) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- 九度OJ 1035:找出直系亲属 (二叉树、递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...
随机推荐
- Codeforces 922F Divisibility (构造 + 数论)
题目链接 Divisibility 题意 给定$n$和$k$,构造一个集合$\left\{1, 2, 3, ..., n \right\}$的子集,使得在这个集合中恰好有$k$对正整数$(x, y) ...
- python安装在windows server2008,故障排除
python安装在windows server2008,故障排除 我也在虚拟机上的win2008安装python2.7.9多次回滚了.搜了一通 Windows Server 2003/2008无法 ...
- j2ee性能调优之最小化资源压力测试法则
前面看到有人讲j2ee的性能调优,虽然这块不是自己的专长,但是猪养多了,也忍不住跳出来说几句. 虽然几乎每本讲性能调优的书籍开篇都会提,没必要的情况下就不要做调优,但是我个人还是认为,所有系统在上线前 ...
- Synchronized 实现原理
记得刚刚开始学习Java的时候,一遇到多线程情况就是synchronized.对于当时的我们来说,synchronized是如此的神奇且强大.我们赋予它一个名字"同步",也成为我们 ...
- HTTP 状态消息 [转]
转自:https://www.cnblogs.com/wuyongyu/p/5745875.html HTTP 状态消息 ...
- iterator取集合元素
1,完整代码 //创建两个arraylist对象 Collection al = new ArrayList(); //al1添加元素 al.add("name1"); al.ad ...
- php的session与免登陆问题
Session 与 Session的GC 由于PHP的工作机制,它并没有一个daemon线程来定期的扫描Session 信息并判断其是否失效,当一个有效的请求发生时,PHP 会根据全局变量 sessi ...
- ArcObject开发时,axtoolbarcontrol中一些添加的按钮是灰色的问题
以Pan按钮为例,当axtoolbarcontrol设置好buddycontrol后,如果你有两个视图的话有些工具pagelayout视图下有用,有些在map视图下有用. 例如,在以下图的方式添加的p ...
- DotnetBrowser入门教程-入门
在.net core时代,web开发基本可以用.net core 2.0取代了.但是在传统领域,桌面开发仍然是不可以抛弃的,譬如: 1.用户需要和串口或者硬件打交道. 2.用户只想简单的安装好就使用, ...
- ylb: SQL表的高级查询-子查询
ylbtech-SQL Server: SQL Server- SQL表的高级查询-子查询 SQL Server 表的高级查询-子查询. 1,ylb:表的高级查询-子查询返回顶部 --======== ...