一、题目

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

二、思路&心得

  • DP问题,先讨论下起始情况:当n = 1的时候,只有1种;当n = 2的时候,有两种;
  • 当n为奇数时,将n - 1的所有情形都加上1即可以得到n时的个数。dp[n] = dp[n - 1];
  • 当n为偶数时,又分为两种情况:若式子含有1,则这种情况的个数为dp[n - 1];若式子不包含1,即全都是偶数,则将所有加数都除以2,可得到个数为dp[n / 2]的值;
  • 题目中要求的输出为实际值的最后九位数字,所以每次计算时要对1000000000取模。

三、代码

#include<stdio.h>
const int MAX_N = 1000005;
const int mod = 1000000000; int dp[MAX_N]; int f(int n) {
for (int i = 3; i <= n; i ++) {
if (i & 1) dp[i] = dp[i - 1];
else dp[i] = (dp[i - 1] + dp[i / 2] ) % mod;
}
return dp[n];
} int main() {
int N;
dp[1] = 1, dp[2] = 2;
while (~scanf("%d", &N)) {
printf("%d\n", f(N));
}
return 0;
}

【动态规划】POJ-2229的更多相关文章

  1. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  2. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  3. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  4. 二分+动态规划 POJ 1973 Software Company

    Software Company Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1112   Accepted: 482 D ...

  5. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  6. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  7. POJ 2229 Sumsets

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 11892   Accepted: 4782 Descrip ...

  8. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  9. DP:Sumsets(POJ 2229)

     数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...

  10. poj 2229 Sumsets DP

    题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...

随机推荐

  1. 新装Linux无法访问域名

    昨天新安装Linux,发现ping百度ping不通: 经查询,得知是系统没有配置DNS域名服务器,百度搜索DNS域名服务器列表: 编辑 /etc/resolv.conf 文件,添加查询到的DNS服务器 ...

  2. python-redis列表模式

    往列表里存放数据先进后出(左进) lpush  names  A  B C D E 往列表里存放数据后进先出(右进) rpush names  G P H K 查看列表里面的数据: lrange na ...

  3. 如何在mac上运行vue项目

    使用终端安装Vue运行环境 1.安装 Homebrew Homebrew 是osx下面最优秀的包管理工具,没有之一.先在终端查看是否已安装brew brew -v 如果返回 Homebrew 的版本号 ...

  4. Vue Router的入门以及简单使用

    Vue Router 是Vue官方的路由管理器,是Vue用来实现SPA的插件.它和 Vue.js 的核心深度集成,让构建单页面应用(SPA)变得易如反掌. 基本概念: 路由:是一种映射关系,是 “pa ...

  5. OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现

    # OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现 [-= 博客目录 =-] 1-学习目标 1.1-本章介绍 1.2-实践内容 1.3-相关说明 2-学习过程 2.1-环 ...

  6. 学习笔记:Oracle的trace文件可见性

    隐藏参数: _trace_files_public 参数 trace文件的默认权限: - r w - r - - - - - 如果设定  trace_files_public参数为 true, 则 t ...

  7. python基础学习1-三元表达式和lambda表达式

    #!/usr/bin/env python # -*- coding:utf-8 -*- 三元运算 if else 的简写 name ="alex" if 1==1 else &q ...

  8. c++ 条件判断

    if语句 认识算术比较 运算符表达式用法 关系运算符表达式 一. 基本的if语句 if (条件成立) { 则执行此语句 }; // ) printf("条件成立"); 二.认识算术 ...

  9. 如何用 Python 爬取需要登录的网站

    [原文地址:]http://python.jobbole.com/83588/ import requests from lxml import html # 创建 session 对象.这个对象会保 ...

  10. 利用开源软件 Hugin 实现照片的景深合成

    利用开源软件 Hugin 实现照片的景深合成 本文主要参考了下面的文章:http://macrocam.blogspot.jp/2013/09/using-hugin-for-focus-stacki ...