• HDU_1709 The Balence (生成函数)


      /*用砝码秤重量,按照左物右码的话,砝码的值可以取负。因为砝码最多就一个,
    所以不许要一般模板里的k 那一重循环,直接去砝码个数为0, 1的两种情况就行。开
    始把重量取负时那种情况想错了,WA了好几次。。。T_T
    */

    //My Code:

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    const int N = 10007;

    int c1[N], c2[N];
    int val[107];

    int main() {
    //freopen("data.in", "r", stdin);

    int n, s, i, j, tmp, cnt;
    while(~scanf("%d", &n)) {
    memset(c1, 0, sizeof(c1));
    memset(c2, 0, sizeof(c2));
    memset(val, 0, sizeof(val));

    for(s = 0, i = 1; i <= n; ++i) {
    scanf("%d", &val[i]);
    s += val[i];
    }
    c1[0] = 1; c1[val[1]] = 1;
    for(i = 2; i <= n; ++i) {
    for(j = 0; j + val[i] <= s; ++j) {
    if(c1[j] != 0) {
    c2[j] += c1[j];
    c2[j+val[i]] += c1[j];
                tmp = j - val[i];   //这里WA了好几次。。。
    tmp = tmp > 0 ? tmp : -1*tmp;
    c2[tmp] += c1[j];
    }
    }
    for(j = 0; j <= s; ++j) {
    c1[j] = c2[j]; c2[j] = 0;
    }
    }
    for(cnt = 0, i = 0; i <= s; ++i) {
    if(!c1[i]) cnt++;
    }
    printf("%d\n", cnt);
    for(j = 0, i = 0; i <= s && j <= cnt; ++i) {
    if(!c1[i]) {
    printf("%d", i); ++j;
    if(j == cnt) putchar('\n');
    else putchar('');
    }
    }
    }
    return 0;
    }



  • 相关阅读:
    Node项目
    Angular模块/服务/MVVM
    Angular介绍1
    Node环境配置及Gulp工具
    Linux及Git介绍
    数据库MySQL
    ReactiveCocoa 监听枚举类型enumerate 或者 NSInteger类型
    ReactiveCocoa 监听布尔(BOOL)类型改变
    python3.7 urlopen请求HTTPS警告'CERTIFICATE_VERIFY_FAILED'解决办法
    Centos yum命令
  • 原文地址:https://www.cnblogs.com/vongang/p/2263763.html
Copyright © 2020-2023  润新知