POJ 2229 递推

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+2+2 
4) 1+1+1+4 
5) 1+2+2+2 
6) 1+2+4

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6

当 i 为奇数时可以将 i-1 的展开项加 1 得到 i 的展开项当 i 为偶数是单纯的将 i-1 的展开项加 1 无法得到所有的 i 的展开项,因为 i 是偶数,所以 i 的展开项中有全为偶数的情况将全为偶数的展开像除以 2 得到的是 i/2 的展开项(可以倒着想,将 i/2 的展开项乘 2 得到 i 的全偶数展开项) 

转移式为 dp[i] = (i&1)?(dp[i-1]):(dp[i-1]+dp[i/2])

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 const int maxn = 1e6+5;
 7 int dp[maxn];
 8
 9 int main(){
10     int n;
11     scanf("%d",&n);
12     memset(dp,0,sizeof(dp));
13     dp[0] = 1;
14
15     for(int i=1;i<=n;++i){
16         if(i&1)
17             dp[i] = dp[i-1];
18         else{
19             dp[i] = dp[i-1] + dp[i>>1];
20             if(dp[i]>1000000000)
21                 dp[i] -= 1000000000;
22         }
23     }
24
25     printf("%d\n",dp[n]);
26     return 0;
27 }

原文地址:https://www.cnblogs.com/kongbb/p/10095563.html

时间: 2024-10-20 06:44:46

POJ 2229 递推的相关文章

POJ 1322 递推+奇偶剪枝

Chocolate Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8800   Accepted: 2306   Special Judge Description In 2100, ACM chocolate will be one of the favorite foods in the world. "Green, orange, brown, red...", colorful sugar-coated

POJ 2506-Tiling(递推+大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7897   Accepted: 3841 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 【完全背包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

POJ 2229 sumset ( 完全背包 || 规律递推DP )

题意 : 给出一个数 n ,问如果使用不同 2 的幂的和来组成这个数 n 有多少种不同的方案? 分析 :  完全背包解法 将问题抽象==>有重量分别为 2^0.2^1.2^2-2^k 的物品且每种物品可无限取,问有多少种方案来填满容量为 n 的背包? 之前并不知道背包还能用来计数....... 有一道裸的背包计数问题可以作为练习 ==> HDU 1284 定义 dp[ i ][ j ] 为前 i 种物品组成总重量 j 的方案数为多少.初始化为 dp[ 0 ][ 0 ] = 1 其他为 0 则状

POJ 1664 放苹果 (递推)

题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] 递推而来. 当盘子的个数大于等于苹果的个数: dp[i - 1][j] :i - 1个盘子放j个苹果,说明i个盘子里最少有一个盘子是空的 dp[i][j - i] :i个盘子都放了苹果,说明有j - i个苹果是随便放置的 否则: dp[i][j] = dp[i - 1][j] 然后没有苹果的盘子的方

POJ 3517 And Then There Was One(约瑟夫环-递推or模拟)

POJ 3517 题目: n  k m 数字1到n成环,先叉数字m,往下数k个,直到最后只有一个数字,输出它. 链表模拟: #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cmath> #include<vector> #incl

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

http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 7 int n; 8 char s[255][255]; 9 10

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不是零种

[ACM] POJ 2506 Tiling (递推,大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 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,