poj 2229 Sumsets

题目链接http://poj.org/problem?id=2229

题目分类:动态规划

代码

分析

1

1

2

1 1

2

3

1 1 1 

1 2

4

1 1 1 1

1 1 2

2 2

4

5

1 1 1 1 1

1 1 1 2

1 2 2

1 4

6

1 1 1 1 1 1

1 1 1 1 2

1 1 2 2

1 1 4

2 2 2

2 4

7

1 1 1 1 1 1 1

1 1 1 1 1 2

1 1 1 2 2

1 1 1 4

1 2 2 2 

1 2 4

可以发现当n是奇数的时候,和n-1的情况是一样的。

当你是偶数的时候  F(n)=F(n-2)+F(n/2)  

F(n-2)代表每组数据中填2个1

F(n/2)代表每个数据都乘以2  

//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>

using namespace std;

int n;
#define MOD 1000000000

__int64 dp[1000009]={0,1,2};

void slove()
{
    for(int i=3;i<=1000002;i++)
    {
        if(i%2)   //奇数
            dp[i]=dp[i-1];
        else      //偶数
        {
            dp[i]=dp[i-2]+dp[i/2];
            if(dp[i]>=MOD)
                dp[i]%=MOD;
        }
    }
}


int main()
{
    slove();
    scanf("%d",&n);
    printf("%I64d
",dp[n]);
    return 0;
}
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
原文地址:https://www.cnblogs.com/gaoss/p/4934666.html