【动态规划】POJ-2229
一、题目
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+1+1+1+1+2
- 1+1+1+2+2
- 1+1+1+4
- 1+2+2+2
- 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的更多相关文章
- poj 2229 【完全背包dp】【递推dp】
poj 2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 21281 Accepted: 828 ...
- poj 2229 一道动态规划思维题
http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...
- poj -2229 Sumsets (dp)
http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...
- 二分+动态规划 POJ 1973 Software Company
Software Company Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1112 Accepted: 482 D ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- poj 2229 Sumsets(dp)
Sumsets Time Limit : 4000/2000ms (Java/Other) Memory Limit : 400000/200000K (Java/Other) Total Sub ...
- POJ 2229 Sumsets
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 11892 Accepted: 4782 Descrip ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- DP:Sumsets(POJ 2229)
数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...
- poj 2229 Sumsets DP
题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...
随机推荐
- awk练习笔记
题目数据如下: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201Susan ...
- Linux中kafka部署和集群
1.下载kafka安装包kafka_2.12-1.1.0. tar -xzvf kafka_2.11-0.8.2.1.tgz #解压 mv kafka_2.11-0.8.2.1 /usr/local/ ...
- linux下pcf8563驱动时钟使用
环境: HelperA64开发板 Linux3.10内核 时间:2019.01.17 目标:PCF8563实时时钟驱动的使用 问题:因为pcf8563的驱动是linux内核自带的,网上也有很多分析的方 ...
- 大数据入门第十天——hadoop高可用HA
一.HA概述 1.引言 正式引入HA机制是从hadoop2.0开始,之前的版本中没有HA机制 2.运行机制 实现高可用最关键的是消除单点故障 hadoop-ha严格来说应该分成各个组件的HA机制——H ...
- VB6 Access 事务(Transaction)
VB6 Access 事务 On Error GoTo err_trans intTrans = conn.BeginTrans '开始事务 X = count For i = 0 To X sql= ...
- 20145209刘一阳《JAVA程序设计》第六周课堂测试
第六周课堂测试 1.现有以下代码,哪些选项插入到第5行可以通过编译?(BDF) 1.import java.util.*; 2. 3.Class FindStuff { 4.public static ...
- python基础学习1-网络爬虫程序中的代理IP设置
#!/usr/bin/env python # -*- coding:utf-8 -*-网络爬虫代理 import urllib.request import random url="htt ...
- 【转载】COM 组件设计与应用(十八)——属性包
原文:http://vckbase.com/index.php/wv/1265.html 一.前言 书接上回,本回着落在介绍属性包 IPersistPropertyBag 接口的实现方法和调用方式.属 ...
- 微信小程序:text元素中加入空格
在text标签中加入 decode = "{{true}}" ,然后字啊需要加入空格的地方使用 即可加入一个空格,可以连续用多个例如: <text decode = &q ...
- java基础---类加载和对象创建过程
类中可以存在的成员: class A{ 静态成员变量: 非静态成员变量: 静态函数: 非静态函数: 构造函数 A(..){...} 静态代码块 static{...} 构造代码块 {...} } 类加 ...