zoukankan      html  css  js  c++  java
  • [HAOI2008]硬币购物(动态规划、容斥、搜索)

    传送

    【题意】4种各有价值的硬币,t组数据,各给出一组硬币的数量,分别求满足给定价值的硬币方案数。

    【解题】

    读完题第一反应:摆花? 尝试写了个搜索,无果。苦思难解,看了题解发现要用完全背包预处理,还有神奇的容斥。emmmmm涨了一波见识

    bj加减符号这个处理还是蛮有意思的  容斥风格; and 我还是想摆花嘤

    #include <bits/stdc++.h>
    #define sc(x) scanf("%lld", &x)
    #define fr(i, n) for (register int i = 1; i <= n; i++)
    #define int long long
    const int N = 100050;
    int t, d[5], c[5], f[N], ans, s;
    void dfs(int x, int k,
             int bj) {  //定义和摆花差不多,不过k这里表示还需要的金额数(倒着dp),bj用来记录加还是减(容斥
        if (k < 0)
            return;
        if (x > 4) {
            ans += f[k] * bj;
            return;
        }
        //容斥容斥容斥
        dfs(x + 1, k, bj);
        dfs(x + 1, k - (d[x] + 1) * c[x], -bj);
    }
    signed main() {  
        fr(i, 4) { std::cin >> c[i]; }
        std::cin >> t;  
        f[0] = 1;
        fr(i, 4) for (int j = c[i]; j <= 100001; j++) f[j] += f[j - c[i]];
        while (t--) {
            ans = 0;
            fr(i, 4) { std::cin >> d[i]; }
            std::cin >> s; 
            dfs(1, s, 1);
            printf("%lld
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    sqli-labs(30)
    sqli-labs(29)
    sqli-labs29-31关Background-6 服务器(两层)架构
    HA高可用的搭建
    克隆虚拟机,如何将克隆虚拟的网卡设置为eth0
    mysql1主多从配置
    关于mysql binlog日志的格式说明
    mysql主从同步
    tomcat的安装
    获取系统的IP
  • 原文地址:https://www.cnblogs.com/phemiku/p/11820020.html
Copyright © 2011-2022 走看看