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 ...
随机推荐
- Oracle ODBC无Oracle连接驱动
.下载odbc驱动 需要下载两个东西 instantclient.zip instantclient.zip 下载地址:http:.html 解压放到同一个目录(无冲突) .将oracle数据库所在电 ...
- Struts2 入门篇
Struts2简介 Struts2 是一个开源.免费的Web框架 官网:http://struts.apache.org/ Struts2.5.2新版本搭建环境更新 导入jar包,必备jar包如下:尤 ...
- Linux Suse 查看wwn号码的方法
查看wwn号码 cat /sys/class/fc_host/host*/port_name *代表全部host目录
- 让Category支持添加属性与成员变量【转载】
Category是Objective-C中常用的语法特性,通过它可以很方便的为已有的类来添加函数.但是Category不允许为已有的类添加新的属性或者成员变量. 一种常见的办法是通过runti ...
- Win8运行金山词霸2005的问题
一般是以下几种状况: 1.运行进入假死 2.取词模块报错 3.词库突然丢失 原因: 文件权限和注册表权限问题 解决方法: 进入"..\Kingsoft\PowerWord 2005\&quo ...
- openwrt修改密码
默认情况下root是没有密码的 需要设置密码后才能开启ssh 修改/etc/shadow文件: root:$1$wEehtjxj$YBu4quNfVUjzfv8p/PBo5.:0:0:99999:7: ...
- nginx反向代理带路径访问问题
nginx的配置为192.168.0.219:80分别映射到upstream组192.168.0.55:8080和192.168.0.206:8080,那如何配置做到访问192.168.0.219:8 ...
- Linux 能PING IP 但不能PING 主机域名的解决方法 vim /etc/nsswitch.conf hosts: files dns wins
Linux 能PING IP 但不能PING 主机域名的解决方法 转载 2013年12月25日 10:24:27 13749 . vi /etc/nsswitch.conf hosts: files ...
- 从零开始学android -- Service
废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...
- Android Studio中常用设置
参考资料: 1.http://blog.csdn.net/itdada/article/details/43375893 常用设置: 1.Tab不用4个空格Code Style->Java-&g ...