【生成函数】Step 4 HDU 1709

BabbleDay posted @ 2013年7月23日 23:02 in 刷题防身 , 558 阅读

详解见:http://bonniebbs.is-programmer.com/posts/40073.html

#include <iostream>
#include <cstring>
#define N 10005
using namespace std;

int abs(int n)
{
    if(n<0) return -n;
    else return n;
}
int main()
{
    string str;
    int c1[N], c2[N], ans[N];
    int n, a, s, cnt;
    while(cin>>n)
    {
        memset(c1, 0, sizeof(c1));
        memset(c2, 0, sizeof(c2));
        memset(ans, 0, sizeof(ans));
        s = cnt = 0;
        cin >> a;
        c1[0] = 1;
        c1[a] = 1;
        s += a;
        for(int i=0; i<n-1; i++)
        {
            cin >> a;
            for(int j=0; j<=s; j++)
            {
                if(c1[j])
                {
                    c2[j+a] += c1[j];
                    c2[j] += c1[j];
                }
            }
            for(int j=1; j<=s; j++)
            {
                if(c1[j])
                {
                    c2[abs(j-a)] += c1[j];
                }
            }
            s += a;
            for(int j=0; j<=s; j++)
            {
                c1[j] = c2[j];
                c2[j] = 0;
            }
        }
        for(int i=0; i<=s; i++)
        {
            if(c1[i]==0) 
            {
                cnt++;
                ans[cnt] = i;
            }
        }
        cout << cnt << endl;
        if(cnt) 
        {
            for(int i=1; i<cnt; i++)
                cout << ans[i] << " ";
            cout << ans[cnt] << endl;
        }
    }
    return 0;
}
Avatar_small
CBSE 2nd Class Solu 说:
2023年9月07日 22:29

CBSE CBSE 2nd Class Solutions 2024 Solutions 2024 Final Exam help Students Prepare for Subject Wise Central Board Exam, our Website Provides Chapter Wise Pdf format Central Board Solution 2024, Both Subject Problems Detailed Solution are Provided, CBSE Solutions 2024 for CBSE Class Solutions 2024 help Students Better Performance in Final Examination 2024, CBSE Solution is the Good Guidance to CBSE 2nd Class Solutions 2024 help you Prepare for CBSE Exam, CBSE Solution are used not only by CBSE but also by all India State Education Boards.CBSE Solutions 2024 for Class has been Solved by the Experience Teachers of CBSE Guide. CBSE Class Books Solutions 2024 are available in PDF format our Website.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter