POJ 2229 Sumsets(递推,思考)

/*

n = 7
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
*/

/*
若i为偶数 : 若有 1 ,至少有两个 ---->f[i-2]的情况+两个1,
若没有1 , 将偶数分解/2-----> f[i/2];
则dp[i] = dp[i / 2] + dp[i-2]

若i为奇数: 必定有 1,
dp[i] = 减去1的序列,加上1
则dp[i] = dp[i – 1]
*/

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4
 5 #include<string>
 6 #include<vector>
 7 #include<set>
 8 #include<map>
 9 #include<queue>
10 #include<math.h>
11
12 #include<algorithm>
13 #include<iostream>
14
15 const int INF = 0x7f7f7f7f;
16 using namespace std;
17 typedef long long ll;
18
19 int dp[1000005];
20 int main(){
21
22     /*
23         若i为偶数 : 若有  1 ,至少有两个    ---->f[i-2]的情况+两个1,
24                     若没有1 , 将偶数分解/2-----> f[i/2];
25           则dp[i] = dp[i / 2] + dp[i-2]
26
27           若i为奇数: 必定有 1,
28             dp[i] = 减去1的序列,加上1
29           则dp[i] = dp[i – 1]
30     */
31     int n;
32     dp[0] = 0;
33     dp[1] = 1;
34     dp[2] = 2;
35     for(int i = 3 ; i <= 1000000 ; i ++){
36         if(i&1) dp[i] = dp[i-1]%1000000000;
37         else dp[i] = (dp[i-2]+dp[i/2])%1000000000;
38     }
39     while(scanf("%d",&n) != EOF && n){
40         printf("%d\n",dp[n]%1000000000);
41     }
42
43     return 0;
44 }
时间: 2024-12-24 18:26:36

POJ 2229 Sumsets(递推,思考)的相关文章

POJ 2229 Sumsets (递推&amp;整数划分变形)

http://poj.org/problem?id=2229 思路:假设加数按从小到大的顺序.当n为奇数时,第一个数必须为1,此时f(n)=f(n-1):当n为偶数时,分两种情况讨论,若第一个数为1,则f(n)=f(n-1),若第一个数不为奇数,则所有数都不为奇数,提出一个公因子2出来,就是f(n/2),所以,f(n)=f(n-1)+f(n/2) 完整代码: /*63ms,4300KB*/ #include<cstdio> const int mod = 1e9; const int maxn

poj 2506 Tiling 递推

题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的状态加上一块竖放2*1的瓷片转移得来,也可以由2*(n-2)的状态加上一块2*2的瓷片或者加上两块横放的2*1的瓷片转移得来. 可得出递推公式:dp[n] = dp[n-1] + dp[n-2]*2: ac秘诀: (1):从输出样例可以看出要用大数来表示,大概需要90位左右. (2):2*0不是零种

POJ 2229 Sumsets(简单DP)

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1 2) 1+1+1+1+1+2 3) 1+1+1

poj 2229 Sumsets 完全背包求方案总数

Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1 2)

POJ 2229 Sumsets(找规律,预处理)

题目 参考了别人找的规律再理解 /* 8=1+1+1+1+1+1+1+1+1 1 8=1+1+1+1+1+1+1+2 2 3 8=1+1+1+1+2+2 8=1+1+1+1+4 4 5 8=1+1+2+2+2 8=1+1+2+4 6 7 8=2+2+2+2 8=2+2+4 8=4+4 8=8 8~9 */ /* 以下引用自博客:http://blog.csdn.net/scorpiocj/article/details/5940456 如果i为奇数,肯定有一个1,把f[i-1]的每一种情况加一个

POJ 2506 Tiling (递推 + 大数加法模拟 )

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7965   Accepted: 3866 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

poj 2229 Sumsets

题目大意: 一个数由2的幂次数的和构成,问有几种构成方式? 主要是找规律 代码如下 1 #include <cstdio> 2 #include <cstring> 3 int n; 4 #define M 1000000000 5 int dp[1000002]; 6 7 int main(int argc, char const *argv[]) 8 { 9 dp[1] = 1; 10 dp[2] = 2; 11 for(int i = 3; i <= 1000000;

poj 2229 Sumsets(dp 或 数学)

Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1 2) 1+1+1+1+

poj 2229 【完全背包dp】【递推dp】

poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 8281 Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an