The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7677    Accepted Submission(s): 3187

Problem Description
Now
you are asked to measure a dose of medicine with a balance and a number
of weights. Certainly it is not always achievable. So you should find
out the qualities which cannot be measured from the range [1,S]. S is
the total quality of all the weights.
 
Input
The
input consists of multiple test cases, and each case begins with a
single positive integer N (1<=N<=100) on a line by itself
indicating the number of weights you have. Followed by N integers Ai
(1<=i<=N), indicating the quality of each weight where
1<=Ai<=100.
 
Output
For
each input set, you should first print a line specifying the number of
qualities which cannot be measured. Then print another line which
consists all the irrealizable qualities if the number is not zero.
 
Sample Input
3
1 2 4
3
9 2 1
 
Sample Output
0
2
4 5
 
Source
 
题意:
n个砝码称重,天平的两端都可以放砝码,问1~sum之间的重量中有多少是这些砝码称不出来的,sum是这些砝码的总重量。
代码:
 //a克砝码有三种状态,放在天平的左端可以认为是x^-a,不放是1,放在右边是x^a,这样(x^-a,1,x^a)。但是负的重量没法用数组存,所以
//可以用abs(k-j)表示左右砝码的差得到c2[abs(k-j)]+=c1[j];
#include<bits\stdc++.h>
using namespace std;
int n,sum,a[],c1[],c2[];
void solve()
{
memset(c1,,sizeof(c1));
memset(c2,,sizeof(c2));
c1[]=;c1[a[]]=; //第一个表达式的系数
for(int i=;i<=n;i++)
{
for(int j=;j<=sum;j++)
{
int k=;
if(k+j<=sum)
c2[k+j]+=c1[j];
k=a[i];
if(k+j<=sum)
c2[k+j]+=c1[j];
int tem=fabs(k-j);
if(tem<=sum)
c2[tem]+=c1[j];
}
for(int j=;j<=sum;j++)
{
c1[j]=c2[j];
c2[j]=;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
solve();
int t=,ans[];
for(int i=;i<=sum;i++)
if(c1[i]==)
{
t++;
ans[t]=i;
}
printf("%d\n",t);
if(t){
for(int i=;i<t;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[t]);
}
}
return ;
}

*HDU 1709 母函数的更多相关文章

  1. The Balance HDU - 1709 母函数(板子变化)

    题意: 现在你被要求用天平和一些砝码来量一剂药.当然,这并不总是可以做到的.所以你应该找出那些不能从范围[1,S]中测量出来的品质.S是所有重量的总质量. 输入一个n,后面有n个数,表示这n个物品的质 ...

  2. HDU 1709 母函数天平问题 可出现减法的情况 The Balance

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. hdu 1028 母函数 一个数有几种相加方式

    ///hdu 1028 母函数 一个数有几种相加方式 #include<stdio.h> #include<string.h> #include<iostream> ...

  4. 组合数学 - 母函数的运用 --- hdu 1709 :The Balance

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. hdu 1709 The Balance(母函数)

    题意: 有一个天平.有N个砝码.重量分别是A1...AN. 问重量[1..S]中有多少种重量是无法利用这个天平和这些砝码称出来的. S是N个砝码的重量总和. 思路: 对于每一个砝码来说,有三种:不放, ...

  6. HDU 2082 母函数模板题

    找单词 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  7. Crisis of HDU(母函数)

    Crisis of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  9. 杭电1171 Big Event in HDU(母函数+多重背包解法)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. javascript的一些知识

    一.Js的this,{},[] this是Javascript语言的一个关键字,随着函数使用场合的不同,this的值会发生变化.但是有一个总的原则,那就是this指的是调用的函数自己. { } 大括号 ...

  2. iOS开发常用校验

    一.身份证号码校验 + (BOOL)cheakIdentityCard: (NSString *)value { value = [value stringByTrimmingCharactersIn ...

  3. Swift - 3.0之GCD学习

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  4. 常用Javascript语法

    1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...

  5. Oracle

    # Oracle 客户端免安装配置 1.  oracle11g   http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1511607 ...

  6. [转]ubuntu 下无法启动chrome

    这很不爽,google了半天也不知道答案(搜索到要重装chrome,可是我怎么都卸载不干净.....),最终解决方法如下: -------------------------------------- ...

  7. Duilib源码分析(六)整体流程

    在<Duilib源码分析(一)整体框架>.<Duilib源码分析(二)控件构造器—CDialogBuilder>以及<Duilib源码分析(三)XML解析器—CMarku ...

  8. css雪碧图生成工具4.2更新

    v4.0更新连接:http://www.cnblogs.com/wang4517/p/4493917.html v4.1更新连接:http://www.cnblogs.com/wang4517/p/4 ...

  9. Python-socket网络编程

    一.计算机网络 多台独立的计算机用网络通信设备连接起来的网络.实现资源共享和数据传递.比如,我们之前的学过的知识可以将D盘的一个文件传到C盘,但如果你想从你的电脑传一个文件到我的电脑上目前是做不到的; ...

  10. html内容超出了div的宽度如何换行让内容自动换行

    在显示评论列表的时候因为有固定宽,但是显示的内容超出的了div的宽,在这种情况下我们需要将其换行,实现的css代码如下   在工作中评论内容测试遇到评论着的评论内容为:"dddddddddd ...