hdu 1059 Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19391 Accepted Submission(s): 5446
Problem Description
Marsha
and Bill own a collection of marbles. They want to split the collection
among themselves so that both receive an equal share of the marbles.
This would be easy if all the marbles had the same value, because then
they could just split the collection in half. But unfortunately, some of
the marbles are larger, or more beautiful than others. So, Marsha and
Bill start by assigning a value, a natural number between one and six,
to each marble. Now they want to divide the marbles so that each of them
gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each
line in the input describes one collection of marbles to be divided.
The lines consist of six non-negative integers n1, n2, ..., n6, where ni
is the number of marbles of value i. So, the example from above would
be described by the input-line ``1 0 1 2 0 0''. The maximum total number
of marbles will be 20000.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output
For
each colletcion, output ``Collection #k:'', where k is the number of
the test case, and then either ``Can be divided.'' or ``Can't be
divided.''.
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided.
Collection #2:
Can be divided.
Source
思路:之前还没看多重背包,就直接把每种物品分为 (背包大小/费用)件,然,我们并不用把每种物品分为费用和权值都相同的多件,为尽量减少分的件数,可每件的费用为 c[i]×2^k, 价值为 w[i]×2^k .......n[i] - 2^k + 1, 如n[i] = 13 时, 可分为 1 , 2, 4, 6 作为系数分别乘以 c[i], w[i]

1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std ; 8 int dp[65000] ; 9 int r[10] ; 10 int sum, num ; 11 int c = 1 ; 12 void _zeroone(int c, int w)//0-1背包处理单件物品 13 { 14 for(int i = sum; i >= c; --i) 15 dp[i] = max(dp[i], dp[i - c] + w) ; 16 } 17 void _compl(int c, int w)//完全背包处理单件物品 18 { 19 for(int i = c; i <= sum; ++i) 20 dp[i] = max(dp[i], dp[i - c] + w) ; 21 } 22 void _mult(int c, int w, int amount) 23 { 24 if(c * amount >= sum) { _compl(c, w) ; return ; }//满足条件时,该件物品可以直接当完全背包时的单件物品处理 25 int k = 1 ; 26 while(k < amount)//二进制分解 27 { 28 _zeroone(c * k,w * k) ; 29 amount -= k ; 30 k = k * 2 ; 31 } 32 _zeroone(amount * c, amount * w) ; 33 } 34 int main() 35 { 36 while(1) 37 { 38 memset(dp, 0, sizeof dp) ; 39 sum = 0 ; 40 int flag = 0 ; 41 for(int i = 1; i <= 6; ++i){ 42 scanf("%d",&r[i]) ; 43 if(r[i] != 0) flag = 1 ; 44 sum += i * r[i] ; 45 } 46 if(!flag) break ; 47 printf("Collection #%d:\n",c++) ; 48 if(sum & 1) { printf("Can't be divided.\n\n") ; continue ;} 49 sum /= 2 ; 50 for(int i = 1; i <= 6; ++i)//将多重背包每件分开转换为完全背包或0-1背包中的单件物品处理 51 _mult(i,i,r[i]) ; 52 if(dp[sum] == sum ) printf("Can be divided.\n") ; 53 else printf("Can't be divided.\n") ; 54 printf("\n") ; 55 } 56 return 0 ; 57 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧