UVALive2953 POJ1775 ZOJ2358 Sum of Factorials【打表+穷尽搜索】

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<=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

Source



Regionals 2003 >> Asia - Guangzhou


问题链接UVALive2953 POJ1775 ZOJ2358 Sum of Factorials

问题简述:输入非负整数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 <iostream>
#include <stdio.h>

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;
}




posted on 2017-06-24 10:40  海岛Blog  阅读(156)  评论(0编辑  收藏  举报

导航