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 }
  • 相关阅读:
    i'm all geared up
    android设置主题和自定义主题的方法
    &和&&的区别
    兼容IE与Firefox的js 复制代码
    实用的注册表单验证代码
    常用JavaScript属性和方法
    400多个JavaScript特效大全
    float引起层飘出父层的解决方法
    JavaScript常见兼容性处理
    多种方法实现checkbox全选、取消全选、删除功能
  • 原文地址:https://www.cnblogs.com/a1225234/p/5173033.html
Copyright © 2011-2022 走看看