POJ 1775 && Zoj 2358 Sum of factorials

Source: Internet
Author: User
Tags mul

Sum of Factorials

Time Limit: 1000MS

Memory Limit: 30000K

Descriptionjohn von Neumann, B. Dec, 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 is 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 T He University of Budapest. His Ph.D. dissertation on set theory is an important contribution to the subject. At the age of, von Neumann proposed a new definition of ordinal numbers, is universally adopted. While still in the He twenties, he made many contributions in both pure and applied mathematics that established him as a mat Hematician 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 on game theory, and with coauthor Oskar Morgenstern he wrote theory of games and economic B Ehavior (1944). There is 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 your to tell him whether or not the number can is expressed by the sum of some facto Rials. Well, it ' s just a piece of cake. For a given n, you ' ll check if there is 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". Inputyou'll get several non-negative integer n (n <= 1,000,000) from input file. Each of the are in a line by itself. The input is terminated by a line with a negative integer. Outputfor each n, you should the print exactly one word ("YES" or "NO") in a. No extra spaces is allowed. Sample Input
9-1
Sample Output
YES

Note 0! = 1;

Depth-First Search

Reference code:

#include <iostream> #include <string.h>using namespace Std;int a[20];bool used[20];void work () {A[0]=1;int mul=1;for (int i=1;a[i-1]<1000000+1;i++) {Mul*=i;a[i]=mul;}} BOOL DFS (int x,int i) {if (A[i]==x | | x==0) return true;if (a[i]>x) return false;for (; i<10;i++) {if (x-a[i]<0) Conti Nue;if (DFS (x-a[i],i+1)) return true; return false;} int main () {work (); int N;while (cin>>n&&n>=0) {if (n==0) {cout<< "NO" <<endl;continue;} if (DFS (n,0) ==true) cout<< "YES" <<endl;elsecout<< "NO" <<ENDL;} return 0;}



POJ 1775 && Zoj 2358 Sum of factorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.