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。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int a[10];
void df(){
for(int i=1;i<10;i++){
a[i]=i*a[i-1];
}
}
int main()
{
a[0]=1;
df();
// for(int i=0;i<10;i++){
// printf("%d\n",a[i]);
// }
int n;
int sum;
while(scanf("%d",&n)==1&&n>=0){
sum=n;
if(n==0){
printf("NO\n");
continue;
}
int flag=0;
for(int i=9;i>=0;i--){
if(a[i]<=sum){
sum=sum-a[i];
// printf("i=%d,%d\n",i,a[i]);
}
if(sum==0){
printf("YES\n");
flag=1;
break;
}
}
if(flag==0)
printf("NO\n");
}
return 0;
}

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. ASP.NET得到系统相关信息

      1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHos ...

  2. android系统体系结构

    android系统底层是建立在Linux系统之上的,如下图 从上图可以看出android系统有五部分组成 1.APPLICATIONS(应用程序层) 包含一些核心应用程序,电子邮件,日历,地图,浏览器 ...

  3. OC细节 - 1.深拷贝与浅拷贝详解

    概述 拷贝:复制一个与源对象内容相同的对象 实现拷贝,需要遵守以下两个协议 NSCopying NSMutableCopying 拷贝返回对象的种类 可变,mutableCopy消息返回的对象 不可变 ...

  4. 配置wamp开发环境【2】 配置wamp开发环境之mysql的配置

    此前我已经将wamp配置的Apache.PHP.phpmyadmin全部配置完成,以上三种配置参照 配置wamp开发环境 下面我们来看看mysql的配置,这里用的是mysql5.5.20,下载地址: ...

  5. PHP 学习笔记 (二)

    PHP中的错误级别: PHP中的报错有3中级别: NOTICE.WARNING.ERROR. NOTICE是级别最轻的一种,一般表示代码不规范,但是程序是可以正常运行的 Warning是比NOTICE ...

  6. MySQL设置

    在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...

  7. iOS 从C移植项目到Objective-C

    一.新建项目 iOS | Framework & Library Cocoa Touch Static Library 新建一个Library库 1. M.h头文件 #ifndef M_h # ...

  8. 好用的自适应表格插件-bootstrap table (支持固定表头)

    最近工作中找到了一款十分好用的表格插件,不但支持分页,样式,搜索,事件等等表格插件常有的功能外,最主要的就是他自带的冻结表头功能,让开发制作表格十分容易,不过网上大多都是英文文档,第一次使用会比较麻烦 ...

  9. Apache 支持PHP

    ①加载PHP模块到Apache中: LoadModule php5_module "d:\php5\php5apache2_2.dll"   ②加入识别扩展名为.php文件(也可以 ...

  10. 那些年优秀的HTML5活动页面

    一个好的手机活动宣传 更能让人分享 传播是爆炸性的 下面是我平时看到一些好的微信活动宣传页面  分享给大家 其中用到的技术 常做微信活动 专题页面的人 可以看看大神们是怎么做的  这样到自己做的时候 ...