51nod 1383 整数分解为2的幂

基准时间限制:1 秒 空间限制:131072 KB 分值: 80  难度:5级算法题
 收藏
 关注
任何正整数都能分解成2的幂,给定整数N,求N的此类划分方法的数量!由于方案数量较大,输出Mod 1000000007的结果。
比如N = 7时,共有6种划分方法。

7=1+1+1+1+1+1+1
  =1+1+1+1+1+2
  =1+1+1+2+2
  =1+2+2+2
  =1+1+1+4
  =1+2+4
Input
输入一个数N(1 <= N <= 10^6)
Output
输出划分方法的数量Mod 1000000007
Input示例
7
Output示例
6


解析:
通过对前几个数打表找规律
f[n]=f[n-1]  -----------------n%2==1
f[n]=f[n-1]+f[n/2]-----------n%2==0
代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;
const LL MAXN=1e6+5;
const LL mod=1000000007;
LL f[MAXN];
void init()
{
    f[1]=1;
    for(LL i=2;i<=MAXN;i++)
    {
        if(i&1)
            f[i]=f[i-1];
        else
            f[i]=f[i-1]+f[i/2];
        f[i]%=mod;
    }
}
int main()
{
    LL n;
    init();
    while(scanf("%d",&n)!=-1)
    {
         cout<<f[n]%mod<<endl;
    }
    return 0;
}

flag:dp方法和母函数方法在寒假攻克
                                                                                                                                                                                    ------------2018/1/23

猜你喜欢

转载自blog.csdn.net/qq_38144740/article/details/79143845