博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive2953 POJ1775 ZOJ2358 Sum of Factorials【打表+穷尽搜索】
阅读量:5858 次
发布时间:2019-06-19

本文共 3504 字,大约阅读时间需要 11 分钟。

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13792   Accepted: 4537

Description

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 contribution 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 Foundations 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 him whether or not the number can be expressed by the sum of some factorials.  
Well, it's just a piece of cake. For a given n, you'll check if there are some xi, and let n equal to Σ
1<=i<=tx
i!. (t >=1 1, xi >= 0, xi = xj iff. i = j). If the answer is yes, say "YES"; otherwise, print out "NO".

Input

You will get several non-negative integer n (n <= 1,000,000) from input file. Each one is in a line by itself.  
The input is terminated by a line with a negative integer.

Output

For each n, you should print exactly one word ("YES" or "NO") in a single line. No extra spaces are allowed.

Sample Input

9-1

Sample Output

YES

Source

 >> 

问题链接:。

问题简述:输入非负整数n,问能否表示成若干个阶乘之和

问题分析

对于输入的n(n <= 1,000,000),如果能够表示成若干个阶乘之和,那么最大的阶乘是多少呢?这需要编写一段程序(函数output_fact())计算一下,结果如下:

0! fact=1 sumfact=1

1! fact=1 sumfact=2
2! fact=2 sumfact=4
3! fact=6 sumfact=10
4! fact=24 sumfact=34
5! fact=120 sumfact=154
6! fact=720 sumfact=874
7! fact=5040 sumfact=5914
8! fact=40320 sumfact=46234
9! fact=362880 sumfact=409114
10! fact=3628800 sumfact=4037914

从上述结果看,和的最大的部分只能是9!。这样事情就简单了,对于输入的n穷尽搜索(从大到小搜索)就可以了。

阶乘值事先打表是必要的,可以避免重复计算。

程序说明:需要注意输入的结束条件,不是0,有点怪(与众不同)。

参考链接:(略)

题记(略)

AC的C++语言程序如下:

/* UVALive2953 POJ1775 ZOJ2358 Sum of Factorials */#include 
#include
using namespace std;const int N = 10;int fact[N+1];void output_fact(int n){ int fact = 1, sumfact = 0; for(int i=0; i<=n; i++) { if(i>0) fact *= i; sumfact += fact; printf("%d! fact=%d sumfact=%d\n", i, fact, sumfact); }}void setfact(int n){ fact[0] = 1; for(int i=1; i<=n; i++) fact[i] = fact[i - 1] * i;}int main(){// output_fact(N); setfact(N); int n; while(~scanf("%d", &n) && n >= 0) { if(n==0) { printf("NO\n"); } else { for(int i=N-1; i>=0; i--) if(n >= fact[i]) n -= fact[i]; if(n == 0) printf("YES\n"); else printf("NO\n"); } } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7563670.html

你可能感兴趣的文章
VBS去除字符串的重复项并统计重复字符出现的次数
查看>>
Linux 进程中 Stop, Park, Freeze【转】
查看>>
YUV格式详解【转】
查看>>
Module Zero之角色管理
查看>>
linux下异步IO的简单例子【转】
查看>>
jQuery 2.0.3 源码分析Sizzle引擎 - 超级匹配
查看>>
python日志模块logging
查看>>
wordpress调用函数大全
查看>>
http协议与http代理
查看>>
JAVA中遇到 UTF-八 序列的字节 1 无效
查看>>
.Net魔法堂:史上最全的ActiveX开发教程——部署篇
查看>>
【译】SQL Server索引进阶第十篇:索引的内部结构
查看>>
.Net开源工作流Roadflow的使用与集成
查看>>
WPF 4 目录树型显示
查看>>
WP7 Emulator 截屏
查看>>
解决方案是什么
查看>>
[LintCode] Move Zeroes 移动零
查看>>
WPF 动态模拟CPU 使用率曲线图
查看>>
16.1。iperf3 - 执行网络吞吐量测试
查看>>
脊柱外科病人资料管理系统的界面设计分析(2)--JOA评分记录的实现
查看>>