zoukankan      html  css  js  c++  java
  • Sumsets(POJ 2229 DP)

    Sumsets
    Time Limit: 2000MS   Memory Limit: 200000K
    Total Submissions: 15293   Accepted: 6073

    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

    分两种情况讨论:
    当n为奇数时,划分中肯定存在1,那么dp[i]=dp[i-1];
    当n为偶数时,可以把划分情况分为含有1和没有1两种情况
      含有1,那么i-1为奇数,dp[i-1];
      不含有1,那么划分中都是偶数,相当于dp[i>>1]的每种划分的两倍;

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 int  M=1000000000;
     6 unsigned long long dp[1000005];
     7 int main()
     8 {
     9     unsigned long long  n,i,j;
    10     scanf("%lld",&n);
    11     dp[1]=1;
    12     for(i=2;i<=n;i++)
    13         dp[i] = (i%2 ?dp[i-1]:(dp[i-1]+dp[i>>1]))%M;
    14     printf("%lld
    ",dp[n]);
    15     return 0;
    16 }
  • 相关阅读:
    CodeGen用户定义的扩展令牌
    CodeGen编写自定义表达式标记
    CodeGen CreateFile实用程序
    CodeGen融合核心关系循环扩展
    CodeGen融合核心扩展定制文件
    CodeGen API分析
    CodeGen字段循环Field Loop
    CodeGen概述
    算子扫描与递归核
    算子本质与数学函数
  • 原文地址:https://www.cnblogs.com/a1225234/p/5173033.html
Copyright © 2011-2022 走看看