HDU 1709 The Balance(母函式)
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8192 Accepted Submission(s): 3430
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
題意:
給你幾個砝碼,一個天平,求出不能稱出的重量,砝碼可以左右放。
POINT:
母函式,其實感覺就是個DP而已。天平可以放兩邊就是減一下。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
const int N = 12000+6;
int c1[N],c2[N];
int a[N];
int main()
{
int n;
while(~scanf("%d",&n))
{
int sum=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),sum+=a[i];
memset(c1,0,sizeof c1);
c1[0]=c1[a[1]]=1;
for(int i=2;i<=n;i++)
{
for(int j=sum;j>=0;j--)
{
if(c1[j]==1)
{
c2[j]=1;
c2[j+a[i]]=1;
c2[abs(j-a[i])]=1;
}
}
for(int j=0;j<=sum;j++) c1[j]=c2[j],c2[j]=0;
}
int cnt=0;
int ans[N];
for(int i=1;i<=sum;i++)
{
if(c1[i]==0)
{
ans[++cnt]=i;
}
}
if(cnt==0) printf("0\n");
else
{
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
{
if(!(i-1)) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
}
return 0;
}
相關文章
- 杭電ACM hdu 2110 Crisis of HDU 解題報告(母函式)ACM函式
- 杭電ACM hdu 1171 Big Event in HDU 解題報告(母函式)ACM函式
- HDU 1028 Ignatius and the Princess III:dp or 母函式函式
- HDU 2082-找單詞(母函式-有限次)函式
- 杭電ACM hdu 2152 Fruit 解題報告(母函式)ACMUI函式
- 杭電ACM hdu 1398 Square Coins 解題報告(母函式)ACM函式
- 母函式詳解和史上最通用最高效的母函式模板函式
- 杭電ACM hdu 2082 找單詞 解題報告(母函式)ACM函式
- HDU 1028-Ignatius and the Princess III(拆分整數-母函式-無限次)函式
- 杭電ACM hdu 2079 選課時間 解題報告(母函式)ACM函式
- SG 函式初步 HDU 1536 && HDU 1944函式
- HDOJ-1398 Square Coins(母函式)函式
- HDU2588GCD(尤拉函式)GC函式
- 杭電ACM hdu 1085 Holding Bin-Laden Captive! 解題報告(母函式)ACMAPT函式
- HDU1729 Stone Game (SG函式)GAM函式
- HDU 4002Find the maximum(尤拉函式)函式
- HDU 母函式簡單題 - 找單詞/Ignatius and the Princess III/Square Coins/Holding Bin-Laden Captive!函式APT
- HDU 1695-GCD(容斥原理+尤拉函式)GC函式
- HDU 3501 Calculation 2 (尤拉函式應用)函式
- HDU 5795 A Simple Nim (SG函式+打表找規律)函式
- HDU3501 Calculation 2(尤拉函式推廣)函式
- HDU 1846-Brave Game(巴什博弈-SG函式)GAM函式
- HDU 1848 Fibonacci again and again(SG函式)AI函式
- HDU 4712Hamming Distance(隨機函式運用)隨機函式
- HDU 2897-邂逅明下(博弈-SG函式打表找規律)函式
- mongodb分片balanceMongoDB
- HDU 1848 Fibonacci again and again (尼姆博弈+sg函式)AI函式
- 邊學邊寫——母函式及其在中學數學競賽中的運用(一)函式
- HDU 1847-Good Luck in CET-4 Everybody!(博弈-SG函式/找規律)Go函式
- MySQL函式大全(字串函式,數學函式,日期函式,系統級函式,聚合函式)MySql函式字串
- Oracle 函式大全(字串函式,數學函式,日期函式,邏輯運算函式,其他函式)Oracle函式字串
- 【函式式 Swift】函式式思想函式Swift
- python中id()函式、zip()函式、map()函式、lamda函式Python函式
- 【函式】Oracle函式系列(2)--數學函式及日期函式函式Oracle
- Python 擴充之特殊函式(lambda 函式,map 函式,filter 函式,reduce 函式)Python函式Filter
- HDFS balance策略詳解
- Oracle RAC Load BalanceOracle
- [CSS] text-wrap: balanceCSS