POJ 2229 递推
2024-09-24 23:22:41
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
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
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 当 i 为奇数时可以将 i-1 的展开项加 1 得到 i 的展开项
当 i 为偶数是单纯的将 i-1 的展开项加 1 无法得到所有的 i 的展开项,因为 i 是偶数,所以 i 的展开项中有全为偶数的情况
将全为偶数的展开像除以 2 得到的是 i/2 的展开项(可以倒着想,将 i/2 的展开项乘 2 得到 i 的全偶数展开项) 转移式为 dp[i] = (i&1)?(dp[i-1]):(dp[i-1]+dp[i/2])
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e6+;
int dp[maxn]; int main(){
int n;
scanf("%d",&n);
memset(dp,,sizeof(dp));
dp[] = ; for(int i=;i<=n;++i){
if(i&)
dp[i] = dp[i-];
else{
dp[i] = dp[i-] + dp[i>>];
if(dp[i]>)
dp[i] -= ;
}
} printf("%d\n",dp[n]);
return ;
}
最新文章
- 在Salesforce中向外公布Service去创建Lead,并且用Asp.Net去调用此Service
- hdu 5929 Basic Data Structure
- 【POJ 1698】Alice&#39;s Chance(二分图多重匹配)
- 关于MVC
- Codeforces Round #204 (Div. 2)->;B. Jeff and Periods
- How to install ffmpeg,mp4box,mplayer,mencoder,flvtool2,ffmpeg-php on centos
- [开源夏令营][四] Docker remote API 之 镜像篇
- [SinGuLaRiTy] ZKW线段树
- Java post提交表单限制
- cmd命令行进入DOS方式编译运行C语言程序实现字符串转换
- ActiveMQ_Windows版本的安装部署
- 第三十篇-ToolBar的使用
- IDAPython学习(二)
- 组件的三大属性state,props,refs与事件处理
- ModuleNotFoundError No module named urllib2
- MySQL报错】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.
- Android Build.VERSION.SDK_INT兼容介绍
- shred_linux_unix
- 普通用户开通sudo权限:xxx is not in the sudoers file.This incident will be reported.的解决方法
- Java CST格式字符串转换成Date类型的数据