zoukankan      html  css  js  c++  java
  • POJ 2229 Sumsets(经典2次幂和问题)

    题意:

    求把一个整数n分解为2的幂的和共有几种方案

    思路:

    1. n为奇数,则肯定其中有1的组成,所以dp[n] = dp[n-1]

    2. n为偶数,根据1的存在划分为2种情况:

       a, 有1的组成,则肯定有2个1,dp[n]的一部分是dp[n-2]

       b, 没有1的组成,这时候如果把组成的每个数都除以2,则dp[n]的另一部分就变成dp[n/2]

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    
    const int M = 1000000000;
    const int MAXN = 1000010;
    
    int dp[MAXN];
    
    int main()
    {
        int n;
        scanf("%d", &n);
        dp[0] = dp[1] = 1;
    
        for (int i = 2; i <= n; ++i)
            if (i & 0x01)
                dp[i] = dp[i-1];
            else
                dp[i] = (dp[i-2] + dp[i>>1]) % M;
    
        printf("%d\n", dp[n]);
        return 0;
    }
    -------------------------------------------------------

    kedebug

    Department of Computer Science and Engineering,

    Shanghai Jiao Tong University

    E-mail: kedebug0@gmail.com

    GitHub: http://github.com/kedebug

    -------------------------------------------------------

  • 相关阅读:
    3.24
    3.23
    构建之法读书笔记2
    寒假学习day23
    寒假学习day22
    寒假学习day21
    寒假学习day20
    寒假学习day19
    寒假学习每周总结4
    寒假学习day18
  • 原文地址:https://www.cnblogs.com/kedebug/p/2803951.html
Copyright © 2011-2022 走看看