51Nod-1383 整数分解为2的幂【数列】

1383 整数分解为2的幂 

任何正整数都能分解成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

问题链接51Nod-1383 整数分解为2的幂

问题分析

  这个问题与OEIS中的一个数列相关联,数列的序号是A018819。该数列的名字就是Binary partition function: number of partitions of n into powers of 2。数列问题的关键是该数列的通项公式,该数列的通项公式是条件递推式。有关该数列的通项公式,参见程序。

扫描二维码关注公众号,回复: 3079851 查看本文章

  这个问题打表是必要的,用函数init()来实现。

程序说明:(无)

题记:(略)

参考链接:(无)

AC的C++语言程序如下:

/* 51Nod-1383 整数分解为2的幂 */

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6;
const int MOD = 1e9 + 7;
int f[N + 1];

void init()
{
    f[0] = 1;
    f[1] = 1;
    for(int i = 2; i <= N; i++)
        if(i % 2)
            f[i] = f[i - 1];
        else
            f[i] = (f[i - 1] + f[i >> 1]) % MOD;
}

int main()
{
    init();

    int n;
    while(~scanf("%d", &n))
        printf("%d\n", f[n]);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/82120115