传送门:http://acm.split.hdu.edu.cn/showproblem.php?pid=1709
描述;
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7613 Accepted Submission(s): 3163
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
Recommend
题意;
给出n个砝码,以及n个砝码的重量,求出【1~sum】中(sum为各砝码总和)不能被称出的重量
思路:
母函数做,这里天平两边都可以放砝码,分两类考虑:
放在同一端:c2[j+k] += c1[j] 不同端c2[abs(j-k)] += c1[j]
然后就做完了
代码:
#include <bits/stdc++.h>
using namespace std;
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)
const int N=105;
int c1[N * N], c2[N * N], val[N], sum, n;
void solve(){
for(int i = 0; i <= val[0]; i += val[0])//先处理第一种砝码,必须从0开始
c1[i] = 1;
rep(i, 1, n - 1){// 对于第一个后面的每种硬币
rep(j, 0, sum) if(c1[j])//枚举已知范围
for(int k = 0; k <= val[i]; k += val[i]){//枚举新增范围
if(j + k <= sum)c2[j + k] += c1[j];
c2[abs(j - k)] += c1[j];
}
// 把当前项保存在c1,清空c2
memcpy(c1, c2, sizeof(c1));
mst(c2, 0);
}
int ans = 0;
rep(i, 0, sum){
if(! c1[i])ans++;
}
printf("%d\n", ans);
rep(i, 0, sum){
if(! c1[i]){
ans--;
if(! ans)printf("%d\n", i);
else printf("%d ", i);
}
}
}
int main(){
while(~scanf("%d", &n)){
sum = 0;
mst(c1, 0);
mst(c2, 0);
rep(i, 0, n-1){
scanf("%d", &val[i]);
sum += val[i];
}
solve();
}
return 0;
}