POJ 2229 Sumsets (完全背包 or 递推)

基础动态规划-POJ2229-计数问题 文章目录基础的动态规划原理实现方法1.记忆化搜索(递归的思想)2.递推关系式3.例子步骤有关计数的动态规划问题POJ 2229解题思路错误的思路正确的思路边界条件实现 基础的动态规划 原理 动态规划的思想的是把一个问题分解为规模小一点的同样的问题,然后找出小规模的问题和大规模问题的联系,从而递归地得出大规模问题的解。 实现方法 1.记忆化搜索(递归的思想) 从动态规划的思想上理解,通过搜索的方式,... 阅读详情
找一些2^x(0<=x),使它们的和为N。比如,N=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 

(1 <= N <= 1,000,000). 最后答案保留最后九位。


思路:

可以用完全背包,d[i][v]  表示前i个数之和为v的最多数量。复杂度O(vn)

还有个更好的思路,f[n]表示和为n的方案数,则有

1. 当 n 为奇数时, f[n] = f[n-1], 因为只需在所有的序列前添加一个 1 即可, 所有的序列同时延迟 1 位, 不会出现重复
2. 当 n 为偶数时, 分为两种情况
  <1> 某个序列首位为1, 则该序列由 f(n-1) 延长而来
  <2> 当某个序列首位为2, 则该序列没有1, 将该序列的所有元素除以 2, 则 是 f(n/2)的序列

      f[n] = f[n-1]+f[n/2]

复杂度O(n)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll mod = 1e9;
const int maxn = 1e6+5;
ll fac[maxn], dp[maxn];

void init()
{
    fac[0] = 1;
    for(int i = 1; i < 20; i++)
        fac[i] = fac[i-1]*2;
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
    for(int i = 0; i < 20; i++)
        for(int j = fac[i]; j < maxn; j++)
            dp[j] = (dp[j]+dp[j-fac[i]])%mod;
}

int main(void)
{
    init();
    int n;
    while(cin >> n)
        printf("%lld\n", dp[n]);
    return 0;
}

/*******************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod = 1e9;
const int maxn = 1e6+5;
int dp[maxn] = {1};

void init()
{
    for(int i = 1; i < maxn; i++)
    {
        if(i%2) dp[i] = dp[i-1];
        else dp[i] = (dp[i-1]+dp[i/2])%mod;
    }
}

int main(void)
{
    int n;
    init();
    while(cin >> n)
        printf("%d\n", dp[n]);
    return 0;
}

poj 2229 Sumsets 【完全背包 or 递推】 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 14968   Accepted: 5978 Description Farmer John commanded his cows to search for different sets of 阅读详情

相关推荐

计量经济学——事件研究法/事件分析法

计量经济学——事件研究法/事件分析法

weixin_46189461的博客 6754

POJ 2229 Sumsets 动态规划 详解

题目链接: POJ—2229 Sumsets 题目大意: 把一个数拆成2的幂的和的形式,求方案总数。比如2,有1+1,还可以2,两种。 题目分析: 其实一开始没发现数学规律,于是打了表(如下图)不难发现,其中的奇数都和前一个偶数一样,而偶数dp[i] = dp[i-1] + dp[i/2]; 解释如下: 10 中1开头的恰好都是9中的数列,不是1开头的最后那几行因为都是偶数,除以二

ChenyangDu的博客 786

SpringBoot+Git+Jenkins+K8s容器化部署

SpringBoot+Git+Jenkins+K8s容器一套部署流程

可乐加.糖 471

poj2229(递推)

题目链接:http://poj.org/problem?id=2229 题目大意:任意给出一个数字n,要求将这个数字分解成若干2的幂数相加,求可以分解的情况数。 解题思路:递推,写出前几个数的分解数后发现规律,若数字为奇数,则情况数等于它前面的那个偶数的情况数,若为偶数,则等于它之前两位的情况数加上它除以2之后的情况数。 AC代码: #include #include using

Tworld 507

Mamba-YOLO实战:环境配置+模型训练+精度调优全流程指南(附Colab代码)

在目标检测领域,一场由状态空间模型引发的技术革新正在悄然改写行业格局。近期国际顶级学术期刊连续发布的YOLOv5_mamba、FER-YOLO等突破性成果,不仅刷新了检测精度记录,更开创了结构化状态空间模型(SSM)与经典检测框架的创新融合范式。当前该领域涌现的十大标杆性研究,已在以下维度取得突破性进展:√ 遮挡目标识别准确率提升38%√ 小样本场景泛化能力增强2.7倍√ 视频流检测延迟降低至17ms√ 多目标交叉干扰抑制效果达SOTA。

2501_90555291的博客 1583

YOLO11训练中的过拟合识别与应对策略:讲解过拟合的表现、原因及应对方法(如数据增强、正则化、DropOut)

摘要 本文深入探讨了YOLO11目标检测模型中的过拟合问题。首先解析了过拟合的本质,即模型在训练数据上表现优异但泛化能力差的现象,并通过误差分解公式解释了其数学原理。文章详细列举了YOLO11中过拟合的具体表现,包括训练与验证指标分化、检测性能不一致性、置信度分布异常和类别检测不均衡等问题,并提供了对应的可视化诊断方法。 在原因分析部分,重点阐述了模型复杂度与数据规模不匹配、训练数据质量与分布问题以及训练策略不当三大根本原因。特别对比了YOLO11不同版本(n/s/m/l/x)的参数量和推荐最小数据量,指出

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 244

POJ2229 (递推)

题目描述: 分析: 这题看起来很简单,实际上真的很简单,但是我花了好长时间都没推出来,我真菜,虽然有了些自己的理解,并且八九不离十,但是就是没有理解好。1.最容易想到的,当i为奇数,dp[i] = dp[i-1],因为再加一个1,都不会再合并成新的序列。 2.当i为偶数是我推了一个多小时没推出来的,看了其他人的题解,发现当有序列首位为1的序列时,就是dp[i],因为再也合不成新的序列了,序列首

karry_zzj的博客 552

基于Spring Boot + Vue 3构建高并发华为数码商城交易平台:全栈架构设计与实现

内容将涵盖系统架构、数据库设计、核心模块开发(用户、商品、订单、购物车)、安全认证、性能优化以及前后端分离部署等关键环节,并提供详尽的代码示例,旨在为开发者提供一个可落地的企业级项目实战指南。<el-button text class="button" @click.stop="addToCart(product)">加入购物车</el-button>.antMatchers("/api/user/login", "/api/user/register").permitAll() // 公开接口。

2201_76125393的博客 306

POJ2229(完全背包 or递推)

题目 POJ2229 新学期开始了,刷题生活又开始了,先从这种半水题开始找感觉 其实是玩了一个寒假,什么都忘了 算法 1.完全背包相信对熟记“背包九讲”的童鞋们都不成问题了,这是一道裸的完全背包问题 2.因为这道题本身题目的特殊性,可以有更加简单高效的解法,只是理解上稍微有一点难度,完整的证明我也是看的别的大神的,递推解法的完整证明 题解 1完全背包 #include&lt;iostream&...

微风的博客 455

13、数据来源介绍与操作指南

本文详细介绍了多种数据源的特点、操作步骤及适用场景,包括元数据数据源、MongoDB数据源、Pentaho Data Integration数据源、OLAP数据源、Pentaho分析数据源和OLAP4J数据源。通过流程图和表格对比,帮助用户理解不同数据源的选择与使用方法,并提供了实际应用案例和注意事项,旨在提升数据获取与分析效率,支持高效报告生成。

ik678901234的博客 153

27930-2015+充电协议解析

对2015+充电与2015充电进行硬件电路对比、通信协议对比和2015+通信协议解析。

manjixingkong1234的专栏 238

POJ2229 Sumsets 递推or完全背包

题目连接:http://poj.org/problem?id=2229 Sumsets Time Limit:2000MS Memory Limit:200000K Total Submissions:26673 Accepted:10093 Description Farmer John commanded his cows to searc...

CharllotteQZY的博客 224

AI Agent智能体在Android开发中的应用

本文探讨了AIAgent(智能代理体)在Android开发中的应用,涵盖技术原理、实践案例与未来趋势。AIAgent通过机器学习、自然语言处理等技术实现代码生成、测试自动化及性能优化,显著提升开发效率。例如,强化学习模型可优化APP响应时间(公式:$\text{响应时间}=f(\text{负载},\text{资源})$),自动化测试覆盖率提升至95%。

郑伟强dev的专栏 124

Sumsets POJ - 2229 (完全背包变形 || 简单dp)

Sumsets POJ - 2229 (完全背包变形 || 简单dp) 题目链接 题目大意:求把一个整数分解为2的幂的和共有几种方案 7=1+1+1+1+1+1+1 7=1+1+1+1+1+2 7=1+1+1+2+2 7=1+1+1+4 7=1+2+2+2 7=1+2+4 输出6 思路:一开始的时候推过,没推出来。后来看了一眼题解,说是完全背包的变形,顿悟。可还是没想出来怎么变...

deerly_的博客 380

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

POJ - 2229 Sumsets 计数dp

传送门:POJ2229 题意:用1,2,4,8这样的2的幂次数去组成n,问有多少种不同的组成方案。 思路:打眼一看这不是完全背包裸题么,写完还真a了,不过总感觉事情没那么简单,一看discuss果然不出我所料。。 解法一:完全背包。 解法二:递推,分析如下: 1. 当 n 为奇数时, f[n] = f[n-1], 因为只需在所有的序列前添加一个 1 即可, 所有的序列同时延迟 1

WA是一笔财富 279

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

poj 2229 Sumsets

http://poj.org/problem?id=2229 参考了:http://www.cnblogs.com/xinsheng/archive/2013/12/07/3462893.html 方法一:完全背包: #include #include static int dp[1000001]; int main(){ int n; while

guojiaqi007的专栏 326

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

POJ 2229 Sumsets (dp)

Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 13788   Accepted: 5480 Description Farmer John commanded his cows to search for different sets of

TaoSama's Microcosm 470

POJ 2229 Sumsets 完全背包问题

dp[n]表示n拆分的个数 假如n为奇数,dp[n] = dp[n - 1] 假如n为偶数 1.拆分方案中有1  dp[n - 1] 2.拆分方案中没有1 dp[n / 2] 所以偶数情况下 dp[n] = dp[n - 1] + dp[n / 2] #include #include #include #include #include #include #include

每天都要进步哦~ 553

POJ - 2229 Sumsets (动态规划)

题目连接 很巧妙的一道题,需要找到奇偶数的递推关系。 1、当N为奇数时:d[N] = d[N-1]; 2、当N为偶数时:d[N] = d[N-1] +d[N/2]; #include #include using namespace std; const int MAX_N = 1000001; int d[MAX_N]; int main() { int N; scanf("

NightWind 1066

POJ 2229 Sumsets——动态规划

此题目采用两种方式解决,一种是完全背包解法,另一种是数学方法

随意凯的博客 257

POJ 2229 Sumsets

DescriptionFarmer 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 num

broken_promise的博客 300

[POJ] 2229 Sumsets

递推关系找到的话,这题就没什么难度了,刚开始我使用的是基于完全背包的改造,结果超时严重,后来搜到了这篇文章,才发现原来区分奇偶性就可以了。。。 唉!分析问题的能力还是太水,继续努力吧~~ 附上我的超时的代码 #include #define N 1000100 #d

天下的技术专栏 620

DP:Sumsets(POJ 2229)

                                  数的集合问题   题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式?   这一题一看,天啦太简单了,完全背包?是不是?   不过的确这一题可以用完全背包来想,但是交题绝对是TLE,如果真的是完全背包的做法那我就不用等那么多天再发这个坑,这一题的确要用到点奇妙的思想。   首先,...

bandilan2400的博客 148

POJ 2229 Sumsets(dp 递推)

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 s

eddddd’ world 416

poj 2229 B - Sumsets

题意:给你一个n,问你有多少个组合加起来可以等于n(组合里的数都是2的幂) 1 <= N <= 1,000,000如: n=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 一共有6种方法一:用完全背包来做 因为n的范围为1000000,然后2的幂,可能取值有20个 然后递推关

bllsll 572
上一篇: 数据结构-顺序队列存储结构的不足(假溢出)
下一篇: 数据库设计的三大范式五大约束
cillyb
博客等级 码龄11年 225粉丝 497原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值