The Balance (hdu1709) 母函数

本文介绍了一种使用多个砝码进行精确药物剂量测量的方法。通过动态规划算法确定无法测量的质量范围,并提供具体实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个砝码,然后问你1-sum那些质量称不出来,

今天就是在刷母函数,复习复习 哈哈

这个我们要多考虑一点 就是砝码可以左右放,也就是多了 tmpp=fabs(j-k*s[i]);tmp[tmpp]+=ans[j]; 然后把减去这种情况想到就好了

#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int s[10005],ans[10005],tmp[10005],cmax; int num[10005],n; int mu() {     memset(ans,0,sizeof(ans));     memset(tmp,0,sizeof(tmp));     ans[0]=1;     cmax=0;     for(int i=1; i<=n; i++)     {         cmax+=s[i];         for(int j=0; j<=cmax; j++)             for(int k=0; k<=1&&k*s[i]+j<=cmax; k++)             {                 int tmpp=fabs(j-k*s[i]);                 tmp[j+k*s[i]]+=ans[j];                 tmp[tmpp]+=ans[j];//注意这里             }         memcpy(ans,tmp,sizeof(tmp));         memset(tmp,0,sizeof(tmp));     }     return 0; } int main() {     while(cin>>n)     {         if(n==0)             break;         int sum=0;         for(int i=1; i<=n; i++)         {             scanf("%d",&s[i]);             sum+=s[i];         }         mu();         int ssum=0;         for(int i=1;i<=sum;i++)         {             if(ans[i]==0)             ssum++;         }         cout<<ssum<<endl;         int k=1;         if(ssum!=0)         {             for(int i=1;i<=sum;i++)             {                  if(ans[i]==0&&k<ssum)                  {                       cout<<i<<" ";                       k++;                  }                  else if(ans[i]==0)                  {                      cout<<i<<endl;                      break;                  }             }         }     }     return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值