• 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;
    }
  • 相关阅读:
    CreateProcess逆向分析3环用户层逆向分析(一)
    CVE20112005(MS11080)漏洞分析
    vmware安装Windows 11虚拟机
    SAP SD 常用配置事物代码
    Java学习笔记02面向对象
    Java学习笔记01基本语法
    Java学习笔记03枚举和注解
    openssl3.0 加密算法库编程精要 06 详解 EVP API 公开密钥密码算法 非对称加密、签名
    零基础实现微信小程序云开发的无人机管理系统项目实例系列教程简述(一)
    chrome安装vuedevtools遇到的常见问题及解决方法
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4295107.html
Copyright © 2020-2023  润新知