The Balance

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

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
 

Mean:

给你N个砝码,每个砝码有自己的重量,现在要你计算从1~S中哪些重量是不能用这些砝码称出来的。(其中S为所有砝码的重量之和)。

analyse:

就是一道母函数的运用题,要注意的是砝码可以摆放在两个托盘上,所以要注意的是两个托盘两边的差值也能称出来的情况,其他的和普通母函数差不多。

Time complexity:O(n^3)

Source code:

// Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-19-11.59
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 10100
#define LL long long
using namespace std; int val[N]; int c1[N],c2[N]; int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
int n;
while(cin>>n)
{
long long sum=0;
for(int i=1;i<=n;++i)
{
cin>>val[i];
sum+=val[i];
}
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
c1[0]=c1[val[1]]=1;
for(int i=2;i<=n;++i)
{
for(int j=0;j<=sum;++j)
{
for(int k=0;k<=1;++k)
{
c2[k*val[i]+j]+=c1[j];
c2[abs(k*val[i]-j)]+=c1[j];
}
}
for(int j=0;j<=sum;++j)
{
c1[j]=c2[j];
c2[j]=0;
}
}
int res[N],ans=0;
for(int i=1;i<=sum;++i)
{
if(!c1[i])
{
res[ans++]=i;
}
}
if(!ans)
{
puts("0");
continue;
}
cout<<ans<<endl;
ans--;
for(int i=0;i<ans;++i)
{
cout<<res[i]<<" ";
}
cout<<res[ans]<<endl;
}
return 0;
}

  

组合数学 - 母函数的运用 --- hdu 1709 :The Balance的更多相关文章

  1. hdu 1709 The Balance(母函数)

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

  2. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

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

  3. hdu 1709 The Balance

    母函数的特殊情况,左右两边都可以放,如样例1,2,9 母函数为(1+x+1/x)*(1+x^2+1/x^2)*(1+x^9+1/x^9) 化简为(1+x+x^2)*(1+x^2+x^4)*(1+x^9 ...

  4. HDU 1709 The Balance( DP )

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

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

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

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

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

  7. *HDU 1709 母函数

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

  8. 组合数学 - 母函数的运用 + 模板 --- hdu : 2082

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. HDOJ 1709 The Balance(母函数)

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

随机推荐

  1. jackson 注脚学习参考

    (1)初级我们从几个简单的使用场景开始:重命名属性,忽略属性,以及修改属性所使用的类型.注意:下面的例子仅仅显示了成员属性(field properties),注解同样也可以用在成员方法(getter ...

  2. Mac OSX - 如何在bash_profile中配置全局环境变量

    我们知道,在Linux操作系统下,配置环境变量是使用如下命令: vim /etc/profile 在OSX下,我们用如下命令打开环境变量配置文件: open ~/.bash_profile 典型的环境 ...

  3. Python的列表推导式

    1.列表推导式书写形式: [表达式 for 变量 in 列表]    或者  [表达式 for 变量 in 列表 if 条件] 2.举例说明: #!/usr/bin/python # -*- codi ...

  4. 关于SQLServer2008数据如何导入SQL2005的解决办法,高版本数据导入低版本中。

    最近需要把SqlServer2008 的数据库导入sqlserver2005 中.直接备份还原肯定不行.后来想到可以生成脚本执行sql语句,并选择数据可以一同进行执行. 点击右键--->任务-- ...

  5. 如何评价微软Connect 2015?[转载]

    全部的新闻和公告在这里:News and Announcements at Connect(); //2015微博话题:Sina Visitor System根据题主的理解大致地总结一下……1. Vi ...

  6. Conway's Game of Life: An Exercise in WPF, MVVM and C#

    This blog post was written for the Lockheed Martin Insight blog, sharing here for the external audie ...

  7. wdlinux 一键安装

    Linux系统(支持CentOS 6.X/7.X.RedHat 6.X.Ubuntu 12.04): ssh登录服务器,执行如下操作即可,需root用户身份安装 wget http://dl.wdli ...

  8. Selenium实战脚本集(4)--简单的开发者头条客户端

    描述 去开发者头条抓取本日的top 10内容,也就是排在前面的10个,需要抓取标题和url 将这些内容保存在数据库,推荐使用sqlite 写个简单的客户端,要求可以展示每日的内容,点击标题后可以打开浏 ...

  9. ORACLE 10g 64位下载地址

    http://download.oracle.com/otn/nt/oracle10g/10201/102010_win64_x64_database.zip http://download.orac ...

  10. MPEG2/TS 包结构简述

    TS 包由以下3部分组成: 包头: 4字节, 同步,识别,检错和加密用 自适配域:可选 包数据:最大184字节(不存在自适配域) 4字节 184字节 包头 自适配域 包数据 4字节包头结构: 1字节 ...