poj 2229 2的次方个数dp
2015-06-24 17:16
288 查看
题意:
给一个数n,求用2的t次方的数来加成这个数,能有几种方法。
结果对1e9取模。
e.g:
7可以有以下6种加法:
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
解析:
当i为奇数的时候:
dp[i] = dp[i - 1]
就是直接+个1就到当前情况了;
当i为偶数的时候:
dp[i] = dp[i - 1] + dp[i>>1]
意思是当i为偶数的时候,拆成一个奇数,还是拆成原来的偶数。
若拆成奇数+1到达当前,则=dp[i - 1],若拆成偶数相加到当前i,则相当于i/2的方案数。
代码:
给一个数n,求用2的t次方的数来加成这个数,能有几种方法。
结果对1e9取模。
e.g:
7可以有以下6种加法:
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
解析:
当i为奇数的时候:
dp[i] = dp[i - 1]
就是直接+个1就到当前情况了;
当i为偶数的时候:
dp[i] = dp[i - 1] + dp[i>>1]
意思是当i为偶数的时候,拆成一个奇数,还是拆成原来的偶数。
若拆成奇数+1到达当前,则=dp[i - 1],若拆成偶数相加到当前i,则相当于i/2的方案数。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> #include <stack> #include <vector> #include <queue> #include <map> #include <climits> #include <cassert> #define LL long long #define lson lo, mi, rt << 1 #define rson mi + 1, hi, rt << 1 | 1 using namespace std; const int maxn = 1e6 + 10; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const double pi = acos(-1.0); const double ee = exp(1.0); LL dp[maxn]; void init() { dp[1] = 1; dp[2] = 2; for (int i = 3; i < maxn; i++) { if (i & 1) { dp[i] = dp[i - 1]; } else { dp[i] = (dp[i - 1] + dp[i >> 1]) % 1000000000; } } } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL init(); int n; while (~scanf("%d", &n)) { printf("%lld\n", dp ); } return 0; }
相关文章推荐
- 矩阵因式分解(LU矩阵分解)与GSL实现
- HR面试五大经典问题和问答
- 如何同时使用多个公秘钥,比如同时使用github和gitoschina
- hbase与mysql整合
- CreateSolidBrush 内存释放
- zabbix监控系统客户端安装
- dbms_sql包的用法
- ssh scp等消除每次问yes/no
- luabind 导出string问题
- OpenGL ES2.0----着色语言
- 洪秀柱于中常会参选理念之说明全文
- 虚拟机安装Ubuntu的上网设置(有线网络和无线网络)
- iOS 延时执行(OC)
- 下载网页文件到本地
- 加载txt文件中的数据到MySQL表中
- 积跬步,聚小流------ps实用小技巧,改变png图标颜色
- 清除浮动的各种方法总结
- 尚学堂Spring视频教程(三):Spring Core中的其他特性
- offsetwidth/clientwidth的区别
- linux proc/net/