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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值