1775 Sum of Factorials 全排列解法+规律解法
2016-06-23 19:40
204 查看
Sum of Factorials Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13391 Accepted: 4430 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 Asia Guangzhou 2003 [Submit] [Go Back] [Status] [Discuss] 来源: http://poj.org/problem?id=1775
//正写 #include <cstdio> #include <iostream> using namespace std; int n,flag,A[10]={1};//在 1! ~ 9! 之间 void DFS(int value,int Set) { if(Set>9||value>n) return; if(value==n) {flag=1;return;} DFS(value+A[Set+1],Set+1); DFS(value,Set+1); //不加下一个 留着给后面加//看了题解 } int main(void) { for(int i=1;i<=9;i++) A[i]=i*A[i-1]; //阶乘表 while(cin>>n) { if(n<0) break; flag=0;DFS(0,-1); printf("%s\n",n!=0&&flag?"YES":"NO"); } } //反写 #include <cstdio> #include <iostream> using namespace std; int n,flag,A[10]={1};//在 1! ~ 9! 之间 void DFS(int value,int Set) { if(Set<0||value>n) return; if(value==n) {flag=1;return;} DFS(value+A[Set-1],Set-1); DFS(value,Set-1); //不加下一个 留着给后面加 } int main(void) { for(int i=1;i<=9;i++) A[i]=i*A[i-1]; //阶乘表 while(cin>>n) { if(n<0) break; flag=0;DFS(0,10); printf("%s\n",n!=0&&flag?"YES":"NO"); } } /*!这个思路非常简单 如果是a!+b!+c! 那么从后往前面减 1! < 2! 1!+2!< 3! 1!+2!+3! < 4! ............... 1!+2!+3!+n! < (n+1)!=(n+1)*n! ------->那么只是从中抽取的元素阶乘和就更小于n! ------->若S=a!+b!+c!+..+j!+k! ,a<b<c..<k 于是直到出现k! 减去它 化成子问题 ------->S' = a!+b!+c!+..+j! ------->最终S能变为0则满足条件 */
#include<iostream> using namespace std; int n,A[10]={1}; int main() { for(int i=1;i<=9;i++) A[i]=i*A[i-1]; //阶乘表 while(cin>>n&&n>=0) { if(n==0){cout<<"NO"<<endl;continue;} for(int i=9;i>=0&&n;i--) if(A[i]<=n) n-=A[i]; n==0?cout<<"YES"<<endl:cout<<"NO"<<endl; } return 0; }
相关文章推荐
- Linux下使用fdisk扩大分区容量
- Hibernate——session(2)
- 在BrodcastReceiver和Service内启动Activit和弹出dialog需要注意的地方
- 编译php7 opcache问题
- 3278 Catch That Cow BFS入门题
- Linux下使用fdisk扩大分区容量
- 可视化日历
- 基础常用的正则表达式
- 解决Linux 软件包的依赖关系
- Hiberbate ——session(1)
- Linux 搭建svn版本库
- 1061 青蛙的约会 扩展欧几里得式 入门题
- 乘积最大子序列
- 3984 迷宫问题 BFS入门题+记录路径
- 删除自身的shell脚本
- class.forName()
- Android 广播接收器
- JQ AJAX
- 45. Jump Game II
- MYSQL数据库导出