poj 2229 动态规划
2015-08-10 10:26
218 查看
Sumsets
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
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
Sample Output
Source
USACO 2005 January Silver
仔细观察数列就会发现,如果n是奇数的话,肯定至少有一个1,去掉每个数列中的一个1,这不就是前一个数的数列个数吗。
所以当n是奇数的时候,划分数是等于n的前一个的。
再来看偶数的情况,偶数有一些存在1,有一些不存在1,可以分成两部分来看啊,
存在1的那一些,不就等于上一个数(必定是奇数)的每个数列都加上个1吗。
不存在1的那一些,肯定都是2的倍数,因此先把他们都除二,这不就是n/2的划分数吗。
Time Limit: 2000MS | Memory Limit: 200000K | |
Total Submissions: 14447 | Accepted: 5763 |
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
Source
USACO 2005 January Silver
仔细观察数列就会发现,如果n是奇数的话,肯定至少有一个1,去掉每个数列中的一个1,这不就是前一个数的数列个数吗。
所以当n是奇数的时候,划分数是等于n的前一个的。
再来看偶数的情况,偶数有一些存在1,有一些不存在1,可以分成两部分来看啊,
存在1的那一些,不就等于上一个数(必定是奇数)的每个数列都加上个1吗。
不存在1的那一些,肯定都是2的倍数,因此先把他们都除二,这不就是n/2的划分数吗。
#include<map> #include<vector> #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<stack> #include<queue> #include<set> #define inf 0x3f3f3f3f #define mem(a,x) memset(a,x,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> pii; inline int in() { int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res; } int dp[1000010]; const int mod=1000000000; int main() { dp[1]=1; for(int i=2;i<=1000000;i++) { if(i&1)dp[i]=dp[i-1]; else dp[i]=(dp[i-1]+dp[i>>1])%mod; } int n; while(~scanf("%d",&n)) { printf("%d\n",dp ); } return 0; }
相关文章推荐
- SDKD Summer Team Contest I
- Android Handler 异步消息处理机制的妙用 创建强大的图片加载类
- Matlab中数组元素引用
- 栈的数组实现
- 单项链表的逆置
- TCP/IP网络编程 学习笔记_8 --优雅地断开套接字连接
- TCP/IP网络编程 学习笔记_8 --优雅地断开套接字连接
- [ios] UI TableView
- 【Axure原型图】——动态面板
- hdu3037 Saving Beans (lucas定理+组合数公式)
- Android studio 安装中遇到一些问题的解决办法,分享一下
- good article
- 漫谈程序员(一)程序员未来发展三大方向
- linux之cut用法
- HorizontalScrollView实现侧滑效果
- 漫谈程序员(一)程序员未来发展三大方向
- 极客编程小挑战#29:实现手机上常见的某一功能的效果显示
- grep命令与正则表达式
- thinking in zz
- android:layout_weight的理解