Sum of Factorials

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

 

 

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
郁闷死了,提交了很多遍老是WA。第一次是TLE,由于下标没弄清楚导致a[i]的值不变老是1,还以为自己的算法问题
然后就是一连串的WA,首先是题目看错了,some xi所以说也是不连续的
1)设i<=n-1,有i!<=(n-1)!,就有0~n-1的阶乘和sum(i!)<=n(n-1)!=n!

2)所以i从大到小,如果i!<n,i!必须用来表示n,因为如果放弃了当前的i!,后面小于i的阶乘的和 <=i! <n(上面公式得出),
然后就是忽略掉0的阶层的情况啊,一晚上被WA所取代。。。。。
/*10!就大于1000000,所以只要把他们存在数组里面,然后一个个找就可以了,要注意题目xi可以是不连续的,那么数是由那几个阶乘的和呢?
数(n-1)!之前的和小于n!,如果一个数大于n!,就一定包含n!相加,因为找不到任何阶乘的和能够大于n!(后面n之后阶乘的和比n!小),只要碰到
n!小于给定的数就相减,如果大于就跳到下一位,知道那个数剩下0,若不为0则输出NO*/
#include<stdio.h>
int main()
{register int i,j;
int n,sum,flag;
int a[11];a[0]=1;
a[1]=1;
for(i=2;i<=10;i++)
a[i]=a[i-1]*i;
while(1)
{scanf("%d",&n);
if(n<0) break;
if(n==0) {printf("NO/n");continue;}
flag=1;
for(i=10;i>=0;i--)
{if(n>=a[i]) {n-=a[i];}
if(n==0) {flag=0;break;}
if(n<0) {flag=1;break;}
}
if(flag) printf("NO/n");
else printf("YES/n");}
return 0;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值