DFS
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7486 Accepted Submission(s): 4578
Problem Description
A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer.
For example ,consider the positive integer 145 = 1!+4!+5!, so it‘s a DFS number.
Now you should find out all the DFS numbers in the range of int( [1, 2147483647] ).
There is no input for this problem. Output all the DFS numbers in increasing order. The first 2 lines of the output are shown below.
Input
no input
Output
Output all the DFS number in increasing order.
Sample Output
1
2
......
所说最后觉得挺水的,但一来看见 [1, 2147483647] 就在想什么比较好的方法可以处理,结果想了半天也没想出来。一看题解,9!=3628801,也就是说10个9也就是36288010,1e7的复杂度,加上中间对每个位的处理,也就是不到1e8的复杂度,而题目给了2000ms,所以可以解决。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int mul[10]; void calcu() { mul[0]=1; for(int i=1;i<=9;i++) mul[i]=mul[i-1]*i; } bool cmp(int x) { int a,tem=0; a=x; do { int mm=a%10; tem+=mul[mm]; a/=10; }while(a>0); if(x==tem) return 1; else return 0; } int main() { long long num1=0,num2=0; calcu(); //cout<<mul[9]; for(int i=1;i<=mul[9]*10;i++) { if(cmp(i)) printf("%d\n",i); } return 0; }