zoukankan      html  css  js  c++  java
  • USACO Subset 整数划分01背包

    又是去理解了一次01背包。

    这道题目的意思就是给你一个N (N < 40)表示有一个集合{1,2,3,... n}

    你要将它划分成相等的两个子集合,求有几种划分方式

    如果N是奇数,那么显然不能由相同的两个Sub Sum组成,所以要输出“0”

    现在我们定义一个数组Dp[i][j] 表示前i个数组合起来的和是j的种数

    接下来就和01背包很像了

    得到状态转移方程Dp[i][j] = Dp[i - 1][j] + Dp[i - 1][j - i]

    分表代表当前的i 取 和 不取

    在每一层 j 的转移下要倒着来,从(1 + n) * n / 2 / 2开始推到1 (如果是从左到右则会重复计算)

    在输出的时候要把答案除以2因为For every Sub sum there're 2 Sub sets

    至此题目已解决。

    Source code:

    /*
    ID: wushuai2
    PROG: subset
    LANG: C++
    */
    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-7      ;
    const int M = 660000         ;
    const ll P = 10000000097ll   ;
    const int INF = 0x3f3f3f3f   ;
    const int MAX_N = 20         ;
    const int MAXSIZE = 101000000;
    
    ll f[2000];
    
    int main() {
        ofstream fout ("subset.out");
        ifstream fin ("subset.in");
        int i, j, k, t, n, s, c, w, q;
        fin >> n;
        int MAX = (n + 1) * n / 2;
        if(MAX & 1){
            fout << "0" << endl;
            return 0;
        }
        MAX /= 2;
        f[0] = 1;
        for(i = 1; i <= n; ++i){
            for(j = MAX; j >= i; --j){
                f[j] += f[j - i];   //Choose and don't choose
            }
        }
        fout << f[MAX] / 2 << endl;//
    
        fin.close();
        fout.close();
        return 0;
    }
  • 相关阅读:
    mybatis关键查询
    智能标签
    mybatis注解基础使用
    myBatis基础知识点
    Mybatis框架模糊查询
    python(7):sympy模块
    anaconda中的包如何传到pycharm中使用?
    python(6):Scipy之pandas
    python(5):scipy之numpy介绍
    python(4): regular expression正则表达式/re库/爬虫基础
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4295107.html
Copyright © 2011-2022 走看看