HDU 1709 The Balance(母函数)

news/2024/7/8 4:20:24 标签: HDU 1709, 母函数

题目:HDU-1085

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085

题目:

The Balance

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


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
 
题目的意思呢,说实话看了很久才看懂,英语差就是心塞。意思是现在有n个砝码,每个质量为a[i],问1~砝码质量之和 不能称出来的重量有几个,有的话输出来。

还是母函数,不过注意因为是天平称东西,所以不仅仅是加,减也是可以得哦~4kg和5kg的砝码也能称出来1kg的物品~~~

看代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<cstdio>
using namespace std;
const int maxn= 10005;
int a[maxn],b[maxn],ans[maxn];
int n,v,t,num;
int main(){
	while(cin>>n){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		cin>>v;
		a[0]=1;
		a[v]=1;
		t=v;
		for(int i=1;i<n;i++){
			cin>>v;
			t+=v;
			for(int j=0;j<=t;j++){
				b[j]+=a[j];
				b[v+j]+=a[j];
				if(j-v>=0)                        //注意这里!
					b[j-v]+=a[j];
				else
					b[v-j]+=a[j];
			}
			for(int j=0;j<=t;j++){
				a[j]=b[j];
				b[j]=0;
			}
		}
		num=0;
		for(int i=0;i<t;i++)                            //输出别输错了按要求来
			if(a[i]==0){
				ans[num]=i;
				num++;
			}
		cout<<num<<endl;
		if(num!=0){
			for(int i=0;i<num-1;i++)
				cout<<ans[i]<<" ";
			cout<<ans[num-1]<<endl;
		}
	}
	return 0;
}

天天向上~~~~加油!


http://www.niftyadmin.cn/n/1306266.html

相关文章

httpwatch使用_使用JavaScript的HTTPWatch自动化

httpwatch使用背景(Background) HTTPWatch (automation)HTTPWatch (自动化)...with PHP (and again and again, and response) ...用PHP (一次又一次&#xff0c;和response )JavaScript shell scripting JavaScript Shell脚本I gave this short presentation at the recent Ya…

WebService之CXF注解之三(Service接口实现类)

ITeacherServiceImpl.java&#xff1a; /*** Title:ITeacherServiceImpl.java* Package:com.you.service.impl* Description:* author:Youhaidong(游海东)* date:2014-5-5 下午11:08:39* version V1.0*/ package com.you.service.impl;import com.you.model.Teacher; import co…

WebService之CXF注解之四(测试类)

TeacherTest.java&#xff1a; /*** Title:TeacherTest.java* Package:com.test.service* Description:* author:Youhaidong(游海东)* date:2014-5-5 下午11:14:09* version V1.0*/ package com.test.service;import org.apache.cxf.interceptor.LoggingInInterceptor; import …

div 自定义画布_通过画布自定义动画光标

div 自定义画布Warning: dont do this. Stop it! Just. Dont. 警告&#xff1a;请勿这样做。 停下来&#xff01; 只是。 别。 So theres this hack by Ben Foxall that shows how you can escape the browser window and draw outside the page. I had to try it myself. So …

U盘重装windows 10 系统教程

其实重装系统是一件很简单的事情&#xff0c;无论是不是计算机专业的学生或者从事计算机有关行业的人&#xff0c;只要是经常操作电脑的人&#xff0c;都可以很快的独立完成。当然&#xff0c;重装系统也有很多种方法&#xff0c;这里介绍的是使用U盘启动项来进行系统重装。操作…

mac上制作9.png_在Mac上安装一堆PNG工具

mac上制作9.pngThis is one of those note-to-self type of posts. Just went through the exercise of installing a number of PNG tools on the Mac and here are some notes. The instructions below should probably work on any unix box. 这是这些自我说明类型的帖子之…

WebService之CXF注解之五(配置文件)

1、web.xml <?xml version"1.0" encoding"UTF-8"?> <web-app version"3.0" xmlns"http://java.sun.com/xml/ns/javaee" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://…

SHUOJ-1847 拉帮结派(并查集)

题目&#xff1a;SHUOJ-1847 题目&#xff1a; 1877: 拉拉帮&#xff0c;结结派 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 49 Solved: 18[Submit][Status][Web Board]Description 武林&#xff0c;当然有拉帮结派&#xff0c;帮派之间还有吞并现象。那么两个人见面&a…