poj 1775 -- Sum of Factorials ( 够坑 )

100W以内,判断一个数是否能表示成几个数阶乘的和 

0! 竟然是1 啊, 渣渣了,阶乘在100W以内的数有10个 , 暴力 复杂度 2^10 , 完全可以接受 , 那个0坑了好久


# include <cstdio>
# include <iostream>
# include <set>
# include <map>
# include <vector>
# include <list>
# include <queue>
# include <stack>
# include <cstring>
# include <string>
# include <cstdlib>
# include <cmath>
# include <algorithm>

using namespace std ;

int st [ 10 ] ;
int head = 0 ;

void getstack ( )
{
    st [ head ++ ] = 1 ;
    int t = 1 ;
    for ( int i = 1 ; ; i ++ )
    {
        t *= i ;
        if ( t > 1000000 )
            break ;
        st [ head ++ ] = t ;
    }
}

bool hash [ 1100000 ] ;

void dfs ( int pos , int sum )
{
    if ( sum > 1000000 )
        return ;
    hash [ sum ] = 1 ;
    if ( pos == head )
        return ;
    dfs ( pos + 1 , sum ) ;
    dfs ( pos + 1 , sum + st [ pos ] ) ;
}

int main ( )
{
    getstack ( ) ;
    dfs ( 0 , 0 ) ;
    hash [ 0 ] = 0 ;
    int n ;
    while ( cin >> n , n >= 0 )
    {
        if ( hash [ n ] )
            cout << "YES" << endl ;
        else
            cout << "NO" << endl ;
    }
}


你可能感兴趣的:(poj 1775 -- Sum of Factorials ( 够坑 ))