【动态规划】POJ-2229
一、题目
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+1+1+1+1+2
- 1+1+1+2+2
- 1+1+1+4
- 1+2+2+2
- 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
二、思路&心得
- DP问题,先讨论下起始情况:当n = 1的时候,只有1种;当n = 2的时候,有两种;
- 当n为奇数时,将n - 1的所有情形都加上1即可以得到n时的个数。dp[n] = dp[n - 1];
- 当n为偶数时,又分为两种情况:若式子含有1,则这种情况的个数为dp[n - 1];若式子不包含1,即全都是偶数,则将所有加数都除以2,可得到个数为dp[n / 2]的值;
- 题目中要求的输出为实际值的最后九位数字,所以每次计算时要对1000000000取模。
三、代码
#include<stdio.h>
const int MAX_N = 1000005;
const int mod = 1000000000;
int dp[MAX_N];
int f(int n) {
for (int i = 3; i <= n; i ++) {
if (i & 1) dp[i] = dp[i - 1];
else dp[i] = (dp[i - 1] + dp[i / 2] ) % mod;
}
return dp[n];
}
int main() {
int N;
dp[1] = 1, dp[2] = 2;
while (~scanf("%d", &N)) {
printf("%d\n", f(N));
}
return 0;
}
【动态规划】POJ-2229的更多相关文章
- poj 2229 【完全背包dp】【递推dp】
poj 2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 21281 Accepted: 828 ...
- poj 2229 一道动态规划思维题
http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...
- poj -2229 Sumsets (dp)
http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...
- 二分+动态规划 POJ 1973 Software Company
Software Company Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1112 Accepted: 482 D ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- poj 2229 Sumsets(dp)
Sumsets Time Limit : 4000/2000ms (Java/Other) Memory Limit : 400000/200000K (Java/Other) Total Sub ...
- POJ 2229 Sumsets
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 11892 Accepted: 4782 Descrip ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- DP:Sumsets(POJ 2229)
数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...
- poj 2229 Sumsets DP
题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...
随机推荐
- 文本处理三剑客之 sed
sed:文本流编辑器 主要是对文件的快速增删改查,查询功能中最常用的是过滤,取行 sed [选项] [sed内置命令字符] [输入文件] Options: -n:取消默认的sed输出,常与sed内置命 ...
- ie中input光标问题
为input 添加 readonly=”readonly” UNSELECTABLE="on" 属性. IE中设置了readonly=”readonly”,点击使用日期选择器 ...
- 学习笔记—MapReduce
MapReduce是什么 MapReduce是一种分布式计算编程框架,是Hadoop主要组成部分之一,可以让用户专注于编写核心逻辑代码,最后以高可靠.高容错的方式在大型集群上并行处理大量数据. Map ...
- Kali渗透测试2-抓包/DNS工具
转载请注明出处. TCPDUMP:命令行网络抓包工具tcpdump -h tcpdump version 4.9.2 libpcap version 1.8.1 OpenSSL 1.1.0h 27 M ...
- WPF-学习笔记 动态修改控件Margin的值
原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...
- Noip前的大抱佛脚----奇技淫巧
STL函数 set set查找前驱后继 multiset<int>::iterator iter; S.insert(x); iter=S.find(x);//返回迭代器 iter--;/ ...
- 13-[CSS]-postion位置:相relative,绝absolute,固fixed,static(默认),z-index
1.postion位置属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- [TJOI2015]概率论[卡特兰数]
题意 \(n\) 个节点二叉树的叶子节点的期望个数. \(n\leq 10^9\) . 分析 实际询问可以转化为 \(n\) 个点的不同形态的二叉树的叶子节点总数. 定义 \(f_n\) 表示 \(n ...
- 使用Sublime Text 3作为React Native的开发IDE
1.下载安装Sublime 3 Sublime 3的下载地址:http://www.sublimetext.com/3 选相应的平台进行下载,安装. 2.安装Package Control 默认的Su ...
- 微信小程序中的 hover-class
微信小程序中,可以用 hover-class 属性来指定元素的点击态效果.但是在在使用中要注意,大部分组件是不支持该属性的. 目前支持 hover-class 属性的组件有三个:view.button ...