poj 2229 Sumsets

http://poj.org/problem?id=2229


参考了:http://www.cnblogs.com/xinsheng/archive/2013/12/07/3462893.html


方法一:完全背包:

#include 
#include 
static int dp[1000001];


int main(){ 
    int n; 
    while(scanf("%d",&n)!=EOF){ 
       memset(dp,0,sizeof(dp));
       dp[0] =1;
       for (int i = 0; i < 21; ++i) { 
           int v = 1<=0){ 
                    dp[j] = (dp[j-v] + dp[j])%1000000000; 
                }       
           }  
       }  
       printf("%d\n",dp[n]); 
    }  
}  

方法二:递推:

1. 当n为奇数时,f(n) = f(n-1), 由前一个偶数最前面加1得到,

2.当n 为偶数时,

2.1.当第一个数为1时,f(n-1)

2.2.当第一个数不为1是,那么剩下的所有数肯定都是2的倍数,有f(n/2)中可能

所以f(n) = f(n-1) + f(n/2)

#include 
static int dp[1000001];

int main(){ 
    int n; 
    while(scanf("%d",&n)!=EOF){ 
        dp[1] = 1;
        for (int i = 2; i <=n; ++i) { 
            if(i&0x1){ 
                dp[i] = dp[i-1]; 
            }else{ 
                dp[i] = (dp[i-1] + dp[i/2])%1000000000; 
            }    
        }  
        printf("%d\n",dp[n]); 
    }  
}  



你可能感兴趣的:(poj 2229 Sumsets)