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. Porter/Duff,图片加遮罩setColorFilter

    同步发表于http://avenwu.net/2015/02/03/porterduff Fork on github https://github.com/avenwu/support 经常会遇到给 ...

  2. distri.lua的web运维工具

    我的新手游项目很快就要进入到寻找发行商的环节,最近几天相对较空闲,逐将工作重心转移到服务器组运维工具的制作上. 回想一年之前经历的那个不算成功的端游项目,因为运维工具设计得不合理,使用十分不方便,游戏 ...

  3. 系统UINavigationController使用相关参考

    闲来无事便在网上google&baidu了一番UINavigationController的相关文章,然后又看了下官方文档:看看更新到iOS7之后UINavigationController的 ...

  4. 解决HP打印机错误:Couldn't open fifo

    我的是因为选错了打印机协议,一开始选成了“互联网打印协议 - IPP”. 解决方案:删除原有打印机配置,重新选择协议为“HP Jetdirect-Socket”即可.

  5. 调用 google speech api (使用Google语音识别引擎)

    完全参考自: http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/ http://aiku.me/bar/104480 ...

  6. [LeetCode] Best Meeting Point

    Problem Description: A group of two or more people wants to meet and minimize the total travel dista ...

  7. Unity3d导出Android的apk文件时相关问题的解决办法

    今天上午着手将一个unity3d开发的小游戏build到android手机上运行,结果遇到了不少问题. 首先遇到的第一个问题是在build到一半的时候,弹出如下报错: Error building P ...

  8. POJ 3519 Minimal Backgammon

    Minimal Backgammon Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1195   Accepted: 700 ...

  9. .woff HTTP GET 404 (Not Found)

    原因:IIS没有添加woff字体的MIME类型,导致HTTP请求404 Not Found错误 解决办法: 1.在web.config中配置 <system.webServer> < ...

  10. Android关于Theme.AppCompat相关问题的深入分析(转)

    http://www.jianshu.com/p/6ad7864e005e 先来看这样一个错误: No resource found that matches the given name '@sty ...