HDU - 2709 Sumsets 【递推】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2709
题意
给出一个数N 要求有多少种方式 求和 能够等于N
加的数 必须是 2的幂次
思路
首先可以想到的是
如果 N 是奇数的话
那么 到达N 的方法数 就是 到达 N - 1 的方法数
因为就相当于 把 所有到达N-1 的方法数 都再 + 1
如果 N 是偶数的话
就有两种情况
0.分解的数字中至少有两个1 那么 dp[n] = 1 + 1 + dp[n - 2]
1.分解的数字中没有1 也就是说 是由dp[n - 2] 中每个分解方式中的数字 都*2 就是 n
所以 dp[n] = dp[n - 2] + dp[n / 2]
AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<list>
#include<stack>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
const int MOD = 1e9;
ll dp[maxn];
void init()
{
dp[0] = 1;
for(int i=1;i<maxn;++i)
{
if(i&1) dp[i]=dp[i-1];
else dp[i]=(dp[i-2]+dp[i/2]) % MOD;
}
}
int main()
{
init();
int n;
while (~scanf("%d", &n))
{
printf("%lld\n",dp[n]%MOD);
}
}
思路二
可以枚举式子中 最大的那位 是多少
比如 最大的那位是 1
dp[1] = 1;
那么
dp[n] = dp[n - 1]
此时表示的状态是 每个到N的方式 组成的数字中 只有1
最大位是2
那么
dp[n] += dp[n - 2]
因为我们是按照 最大数 枚举上去的
而不是 按照 之前的状态转移的
比如 最大数是2
那么 dp[n] 就要加上 dp[n - 2] dp[n - 2] 表示 最大数是2 来到达n - 2 的方式总数
而不是 到达 n - 2 的所有方式
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
const int MOD = 1e9;
int binary[25];
int dp[maxn];
void init()
{
CLR(binary, 0);
CLR(dp, 0);
binary[0] = 1;
dp[0] = 1;
for (int i = 1; i < 25; i++)
binary[i] = binary[i - 1] * 2;
for (int i = 0; i < 25 && binary[i] < maxn; i++)
{
for (int j = binary[i]; j < maxn; j++)
{
dp[j] += dp[j - binary[i]];
dp[j] %= MOD;
}
}
}
int main()
{
init();
int n;
while (~scanf("%d", &n))
cout << dp[n] % MOD << endl;
}
HDU - 2709 Sumsets 【递推】的更多相关文章
- hdu2709 Sumsets 递推
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2709 感觉很经典的一道递推题 自己想了有半天的时间了....比较弱.... 思路: 设f[n]表示和为 ...
- HDU 4747 Mex 递推/线段树
题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...
- 致初学者(四):HDU 2044~2050 递推专项习题解
所谓递推,是指从已知的初始条件出发,依据某种递推关系,逐次推出所要求的各中间结果及最后结果.其中初始条件或是问题本身已经给定,或是通过对问题的分析与化简后确定.关于递推的知识可以参阅本博客中随笔“递推 ...
- poj2229 Sumsets (递推)
http://poj.org/problem?id=2229 看到题目能感觉到多半是动态规划,但是没有清晰的思路. 打表找规律: #include<cstdio> #include< ...
- HDU 2604 Queuing(递推+矩阵)
Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 1723 DP/递推
题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...
- hdu 1249 三角形 (递推)
三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU 5366 dp 递推
The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- 升级滑动销毁activity,随着手势的滑动而滑动的效果
文章开头先注明本滑动销毁是对 http://blog.csdn.net/xiaanming/article/details/20934541 这篇博客的内容进行一个小小的改动 添加向左滑动打开另外一个 ...
- 【DB2】db2命令Export与Import
环境准备 1.新建表 qinys@Linux:~> db2 "create table tb1(id int,dt timestamp,name varchar(100))" ...
- hadoop:WordCount问题总结
今天玩了一下hadoop的MapReduce,中途遇到了几个问题,在此记录一下. 1.一切按照配置完成之后,hadoop namenode format,start-all.sh启动,使用jps查看进 ...
- smali 语法参考
原文见:http://www.blogjava.net/midea0978/archive/2012/01/04/367847.html Dalvik opcodes Author: Gabor Pa ...
- RealProxy AOP过滤方法的参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 禁止"Windows Media Player Network Sharing Service"服务自动启动
开始 -> 运行 -> gpedit.msc -> 计算机配置 -> 管理模板 -> Windows 组件 -> Windows Media Player -> ...
- [译]GLUT教程 - 笔划字体
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Stroke Fonts 笔划字体是用线条生成的.跟位图字体相反,笔划字 ...
- PHP将多级目录打包成zip文件
最近接触PHP,需要用到zip压缩,在网上搜索的一大堆,发现代码都不低于50行. 而且调用还很费事(基础太少看不懂).让我收获的是Php提供有一个ZipArchive类,并有如下方法. bool a ...
- spring roo初体验
1.下载spring-roo-2.0.0.M1,并执行如下命令,在/usr/local/bin下面建立一个roo的软连接 sudo ln -s /Users/pud/Documents/still ...
- git学习之时光机穿梭(四)
时光机穿梭 我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed ver ...