HDU1709 天平

来源:互联网 发布:mac 股票行情软件 编辑:程序博客网 时间:2024/04/29 02:57

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1709

给你一些砝码的质量,求出1到总质量中不能表达的质量的个数和具体的值。

思路:和普通的母函数不一样的地方就是砝码可以放在重物那一边,所以多了个c2[abs(j-k)]+=c1[j];

#include<iostream>#include<math.h>#include<string.h>using namespace std;#define maxn 10110int c1[maxn], c2[maxn];int main(){    int T, i, j; int a[200],maxsum=0;    while (cin >> T)     {        maxsum = 0;        memset(a, 0, sizeof(a));        memset(c1, 0, sizeof(c1));        memset(c2, 0, sizeof(c2));        for (i = 1; i <= T; i++)        {            cin >> a[i];            maxsum += a[i];        }        c1[a[1]] = 1;        c1[0] = 1;        int m = a[1];//表示当前的最高项        for (i = 2; i <= T; i++)        {            for (j = 0; j <= m; j++)                for (int k = 0; k <= a[i]; k += a[i])                {                    c2[j + k] += c1[j];                    c2[abs(j - k)] += c1[j];                }                    m += a[i];                    for (j = 0; j <= m; j++)                    {                        c1[j] = c2[j];                        c2[j] = 0;                    }        }        int ans = 0,q[maxn];        for (i = 0; i <= maxsum; i++)        {            if (c1[i] == 0)                q[ans++] = i;        }        cout << ans << endl;        if (ans != 0)        {            int mm;            for ( mm = 0; mm < ans - 1; mm++)            {                cout << q[mm] << " ";            }            cout << q[mm]<<endl;        }            }    return 0;}


原创粉丝点击