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. Halcon标定步骤

    Halcon标定步骤 1.设置相机内部参数的初始值 StartCamPar := [0.016,0,0.0000074,0.0000074,326,247,652,494]set_calib_data ...

  2. Scala深入浅出实战经典之 List的foldLeft、foldRight、sort操作代码实战

     Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 腾讯微云:http://url.cn/TnGbdC 3 ...

  3. Excel 二级下拉菜单

    http://jingyan.baidu.com/article/cd4c2979f31967756f6e6066.html http://hi.baidu.com/chenshake/item/e1 ...

  4. 前沿技术解密——VirtualDOM

    作为React的核心技术之一Virtual DOM,一直披着神秘的面纱. 实际上,Virtual DOM包含: Javascript DOM模型树(VTree),类似文档节点树(DOM) DOM模型树 ...

  5. 如何让Targetprocess 中 webhook 推送comment 到指定的项目

    Targetprocess 作为Agile管理工具非常好使.我们用TP + bearychat来做任务的沟通. TP目前是不支持comment push到指定的项目,因为其带出的字段中没有相关项目.但 ...

  6. jenkins svn E175002错误

    JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dsvnkit.http.sslProtocols='SSLv3' -Dsvnkit.http ...

  7. 修改eclipse启动时eclipse使用的jre

    eclipse在启动的时候,和环境变量中的jdk不兼容,可以单独制定eclipse运行的jre. 方法: 在eclipse的配置文件里增加-vm参数即可. 打开eclipse目录下的eclipse.i ...

  8. 分享7款非常实用的jQuery/CSS3插件演示和源码

    上次我们分享了15款效果很酷的最新jQuery/CSS3特效,非常不错,今天要分享7个非常实用的jQuery/CSS3插件演示和源码,一起来看看. 1.jQuery ajax点击地图显示商家网点分布 ...

  9. 自动化测试管理平台ATMS(V2.0.3_8.28)下载

    自动化测试管理平台ATMS(V2.0.3_8.28)下载http://automationqa.com/forum.php?mod=viewthread&tid=2845

  10. android中的提示信息显示方法(toast应用)

    android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...