您的位置:首页 > 其它

poj 2229 动态规划

2015-08-10 10:26 218 查看
Sumsets

Time Limit: 2000MSMemory Limit: 200000K
Total Submissions: 14447Accepted: 5763
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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: