HDUOJ ----1709
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4737 Accepted Submission(s): 1895
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
http://acm.hdu.edu.cn/showproblem.php?pid=1709
母函数,有左右算法....很好的题目..
思路...先选着一个点作为‘0’点,然后对左右放置之后的数组(即幂)进行右移动.....最后统计.....很吊的一道母函数呀!!,题目非常新颖.....
1 #include<iostream>
2 #include<vector>
3 #include<cstring>
4 #define maxn 20003
5 #define gong 10000
6 using namespace std;
7 int c1[maxn],c2[maxn];
8 int save[maxn];
9 int main()
10 {
11 int n,i,sum,j,k,count;
12 while(cin>>n)
13 {
14 vector<int>arr(n);
15 memset(c1,0,sizeof(c1));
16 memset(c2,0,sizeof c2);
17 memset(save,0,sizeof save);
18 count=sum=0;
19 for(i=0;i<n;i++)
20 {
21 cin>>arr[i];
22 sum+=arr[i];
23 }
24 for(i=-arr[0];i<=arr[0];i+=arr[0])
25 {
26 c1[i+gong]=1;
27 }
28 for(i=1;i<n;i++)
29 {
30 for(j=-sum;j<=sum;j++)
31 {
32 for(k=-arr[i];k<=arr[i]&&k+j<=sum;k+=arr[i])
33 {
34 c2[k+j+gong]+=c1[j+gong];
35 }
36 }
37
38 for(j=-sum+gong;j<=sum+gong;j++)
39 {
40 c1[j]=c2[j];
41 c2[j]=0;
42 }
43 }
44 int ans=0;
45 for(j=0;j<=sum;j++)
46 {
47 if(c1[j+gong]==0||c1[gong-j]==0)
48 {
49 save[ans++]=j;
50 }
51 }
52 cout<<ans<<endl;
53 if(ans)
54 {
55 for(i=0;i<ans;i++)
56 {
57 if(i==0)
58 cout<<save[i];
59 else
60 cout<<" "<<save[i];
61 }
62 cout<<endl;
63 }
64 }
65 return 0;
66 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- php fread函数使用方法总结
- Yii2框架控制器、路由、Url生成操作示例
- Laravel框架实现调用百度翻译API功能示例
- phpstudy2018升级MySQL5.5为5.7教程(图文)
- laravel实现简单用户权限的示例代码
- tp5(thinkPHP5框架)时间查询操作实例分析
- PHP使Laravel为JSON REST API返回自定义错误的问题
- 详解PHP PDO简单教程
- Python实现ElGamal加密算法的示例代码
- PHP实现基于状态的责任链审批模式详解
- django rest framework使用django-filter用法
- 通过实例解析python创建进程常用方法
- thinkPHP5框架实现多数据库连接,跨数据连接查询操作示例
- OpenCV 之按位运算举例解析
- Python实时监控网站浏览记录实现过程详解