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<=txi!. (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

注意:0的阶乘是1;(输入0,输出NO)

不是输入-1结束,而是输入负数结束程序;

题目要求不是连续阶乘和;

例如:4=0!+1!+2!=1+1+2

输出 YES;

7=3!+1!;

输出 YES;

我这里的思路是:

从不大于n的最大数开始减,如果能减到n为0,输出YES,否则

输出NO。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. int a[10];
  5. void df(){
  6. for(int i=1;i<10;i++){
  7. a[i]=i*a[i-1];
  8. }
  9. }
  10. int main()
  11. {
  12. a[0]=1;
  13. df();
  14. // for(int i=0;i<10;i++){
  15. // printf("%d\n",a[i]);
  16. // }
  17. int n;
  18. int sum;
  19. while(scanf("%d",&n)==1&&n>=0){
  20. sum=n;
  21. if(n==0){
  22. printf("NO\n");
  23. continue;
  24. }
  25. int flag=0;
  26. for(int i=9;i>=0;i--){
  27. if(a[i]<=sum){
  28. sum=sum-a[i];
  29. // printf("i=%d,%d\n",i,a[i]);
  30. }
  31. if(sum==0){
  32. printf("YES\n");
  33. flag=1;
  34. break;
  35. }
  36. }
  37. if(flag==0)
  38. printf("NO\n");
  39. }
  40. return 0;
  41. }

POJ 1775 (ZOJ 2358) Sum of Factorials的更多相关文章

  1. poj 2246 (zoj 1094)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1094 ZOJ Problem Set - 1094 Matrix Chai ...

  2. POJ 2260(ZOJ 1949) Error Correction 一个水题

    Description A boolean matrix has the parity property when each row and each column has an even sum, ...

  3. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  4. poj 3122 (二分查找)

    链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  5. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  6. POJ 3252 (数位DP)

    ###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...

  7. poj 3335(半平面交)

    链接:http://poj.org/problem?id=3335     //大牛们常说的测模板题 ------------------------------------------------- ...

  8. Sumdiv POJ - 1845 (逆元/分治)

    Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...

  9. Scout YYF I POJ - 3744(矩阵优化)

    题意:一条路上有n个地雷,给出地雷的位置.某人从起点(位置1)出发,走一步的概率是p,走两步的概率是(1-p),然后问有多少概率走过这个雷区. 思路: 只要走过最后一个地雷就代表走过雷区了. 而每到 ...

随机推荐

  1. 《将博客搬至CSDN》的文章

    我的CSDN地址 博客园应该以后会很少来了.

  2. oracle数据库误删恢复方法

    一.如果只是误删部分数据或者某条数据可以通过 1.select * from 误删除的表明 as of timestamp to_Date('恢复年月日  时分秒', '恢复时间格式')       ...

  3. C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN

    C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN.NET 在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够 ...

  4. MySQL 时间戳(Timestamp)函数

    1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp() mysql> select current_timestamp, curren ...

  5. 分享一个自己写的基于TP的关系模型(三)

    这段时间对模型做了升级和优化,并将版本更新到TP3.2. 下载 下载后请将目录放置TP的Library目录下 1.数据节点优化,原来的节点为模型的名称或者表名,现在更新为定义关系的方法名 public ...

  6. CSS鼠标点击式变化图片透明度

    今天分享前端代码主题:jequery控制css图片透明度 很多时候在网站图片处理上需要实现一些辅助效果,比如鼠标在图片上滑动时或点击时改变图片颜色(变灰或者其他),其实一个简单的办法就是改变图片css ...

  7. ERP系统开发平台 (C#语言,支持多数据库)

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 适用软件:适合开 ...

  8. MAC 使用Jetbrains's产品

    Jetbrains's MAC 使用 ./gradle fatjar 或者 ./gradlew.sh fatjar java -jar build/lib/xx.jar 链接: http://pan. ...

  9. php100视频教程解压密码

    php100-75-vip.rar 解压密码:php100-18293-2938-2839-348-#php100-76_u.rar 解压密码:php100-18634-6254-1001-283-# ...

  10. iOSシステム構成の纏め

    iOSのアーキテクチャ a)       iOSのアーキテクチャは階層化されている最上位レベルでは.iOSはそれが稼働しているハードウェアとアプリケーションの間の仲介役を果たします.アプリケーションが ...