当前位置:网站首页>拆分整数为2的幂次项和 → 理解多重背包问题二进制优化的核心思想
拆分整数为2的幂次项和 → 理解多重背包问题二进制优化的核心思想
2022-08-10 15:25:00 【hnjzsyjyj】
【题目描述】
输入一个整数,输出为2的幂次项和的形式。
【输入样例】
26
【输出样例】
26=1+2+4+8+11
【算法分析】
理解了此题,就明白了多重背包问题的二进制优化的主要思想。
多重背包问题的二进制优化思想解析及题目详见 https://blog.csdn.net/hnjzsyjyj/article/details/126190151
为了方便说明问题,此处简述多重背包问题的二进制优化思想如下:
多重背包问题通常可转化成01背包问题求解。但若将每种物品的数量拆分成多个1的话,时间复杂度会很高,从而导致TLE。所以,需要利用二进制优化思想。即:一个正整数n,可以被分解成1,2,4,…,2^(k-1),n-2^k+1的形式。其中,k是满足n-2^k+1>0的最大整数。
例如,假设给定价值为2,数量为10的物品,依据二进制优化思想可将10分解为1+2+4+3,则原来单个价值为2,数量为10的物品可等效转化为价值分别为1*2,2*2,4*2,3*2,即价值分别为2,4,8,6,数量均为1的物品。
.
【算法代码一】
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
cout<<n<<"=";
int k=1;
while(k<=n) {
if(k==n) cout<<k;
else cout<<k<<"+";
n=n-k;
k=k*2;
}
if(n>0) {
cout<<n;
}
return 0;
}
/*
in1:
26
out1:
26=1+2+4+8+11
in2:
7
out2:
7=1+2+4
*/
【算法代码二】
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
cout<<n<<"=";
int t=0;
int k=1;
while(k<=n) {
if(k==n) cout<<"2^"<<t;
else cout<<"2^"<<t<<"+";
n=n-k;
k=k*2;
t++;
}
if(n>0) {
cout<<n;
}
return 0;
}
/*
in1:
26
out1:
26=2^0+2^1+2^2+2^3+11
in2:
7
out2:
7=2^0+2^1+2^2
*/
【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/126190151
边栏推荐
猜你喜欢
随机推荐
const-modified pointer variable (detailed)
Community News——Congratulations to Dolphin Scheduling China User Group for 9 new "Community Administrators"
[Data warehouse design] Why should enterprise data warehouses be layered?(six benefits)
It is reported that the original Meitu executive joined Weilai mobile phone, the top product may exceed 7,000 yuan
E. Cross Swapping (and check out deformation/good questions)
第贰章模块大全之《 collections模块》
Cesium快速上手4-Polylines图元使用讲解
Mysql statement analysis, storage engine, index optimization, etc.
【每日一题】【leetcode】26. 链表-链表中倒数第k个节点
12海里、24海里、200海里的意义及名称
fuse简介
scala 10种函数高级应用
photoshop入门教程
Please check the preparation guide for the 2022 Huawei Developer Competition
数据在内存中的存储
Methodology of multi-living in different places
FP6378AS5CTR SOT-23-5 高效1MHz2A同步降压调节器
请查收 2022华为开发者大赛备赛攻略
2022 CCF中国开源大会会议通知(第四轮)
LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子