【动态规划】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+ ...
随机推荐
- linux查看磁盘占用情况
一:首先是先登录 二:查看当前目录 命令:df -h 三:查看具体文件夹占用情况 命令:du --max-depth=1 -h /data/ 或者:为了快算显示,同时也只是想查看目录整体占用大小 命 ...
- BZOJ1190_梦幻岛宝珠_KEY
题目传送门 观察数据a*2^b,转化成二进制后,后面跟了b位的0,可以转化为一个分层背包. 先预处理出每个物品是哪一层的,并放在同层内DP. 同层内直接背包,考虑层与层之间的DP. 第一维枚举层数,然 ...
- 对Dataguard的三种模式的理解
模式1:最大可保护模式: 必须同步. 模式2:最大可用性模式: 能同步就同步,不能同步就不同步. 模式3:最大性能模式: 异步模式.
- 【转载】基于MFC的ActiveX控件开发(3)
原文:http://iysm.net/?p=122 3.事件 ActiveX 控件使用事件通知容器控件上发生了某些事情.事件的常见示例包括单击控件.使用键盘输入数据和控件状态更改.当发生这些操作时,控 ...
- nginx后端节点健康检查
一.nginx健康检查的三种方式 .ngx_http_proxy_module 模块和ngx_http_upstream_module模块(自带) 官网地址:http://nginx.org/en/d ...
- underscore.js 分析6 map函数
作用:通过转换函数(iteratee迭代器)映射列表中的每个值产生价值的新数组.iteratee传递三个参数:value,然后是迭代 index. _.map([1, 2, 3], function( ...
- jquery inArray()函数详解
jquery inarray()函数详解 jquery.inarray(value,array)确定第一个参数在数组中的位置(如果没有找到则返回 -1 ). determine the index o ...
- XAF-如何实现自定义权限系统用户对象
本示例使用XPO. 新建一个XAF项目.填加两个类进来: [DefaultClassOptions] public class Employee : Person { public Employe ...
- kali 2018.1安装教程
00x01 摘要 Kali-linux系统,渗透测试人员的利器,其官网自称 OurMost Advanced Penetration Testing Distribution, Ever. 永远是最 ...
- python simple factory mode example
Two python simple factory mode examples shown in this section. One is for base operation and another ...