当前位置: 首页 > news >正文

【Luogu】动态规划六

P1586 四方定理 - 洛谷

思路:

这题其实就是完全背包问题,但是有限制,最多数量只能是 4

所以我们可以定义 dp[i][j] 为 i 用 j 个数拼凑的总方案数

那么转移方程也很明显了,dp[i][j] += dp[i - k*k][j - 1]

具体的,我们用三层循环,一层用于枚举物品(数字),一层用于枚举价值 i,一层用于枚举个数 j

注意初始化 dp[0][0] = 1

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include <iomanip>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <array>
#include <tuple>
using namespace std;
#define int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
const int MaxN = 32768;
int f[MaxN + 1][5];void Init()
{f[0][0] = 1;for (int i = 1; i <= sqrt(MaxN); i++){for (int j = i*i; j <= MaxN; j++){for (int k = 1; k <= 4; k++){f[j][k] += f[j - i*i][k - 1];}}}
}void solve()
{int n;cin >> n;cout << f[n][1] + f[n][2] + f[n][3] + f[n][4] << endl;
}
signed main()
{Init();cin.tie(0)->sync_with_stdio(false);int t = 1;cin >> t;while (t--){solve();}return 0;
}

P1504 积木城堡 - 洛谷

思路:

很无聊的一题,n个01背包

题目意思就是让我们找到一个最大的每个 01 背包都能拼出的 i

输入很石,要自己判断结束

那我们对于每个背包都做一遍枚举,经典的 能否拼凑xx 问题,用bool即可,然后枚举完后我们检测其中所有的 i,如果是 1,那么存入 tot[i],其中 tot[i] 代表 i 能拼凑出来的总次数

最后枚举 i,如过存在 tot[i] == n ,说明每个背包都能拼出 i,直接输出即可 

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include <iomanip>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <array>
#include <tuple>
using namespace std;
#define int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endlvoid solve()
{int n;cin >> n;vector<int> tot(10005, 0);for (int i = 0; i < n; i++){vector<int> now,dp(10005,0);int x,len = 0;while(1){cin >> x;if (x == -1){break;}now.push_back(x);len += x;}dp[0] = 1;for (int j = 0; j < now.size(); j++){for (int k = len; k >= now[j]; k--){dp[k] |= dp[k - now[j]];}}for (int j = len; j >= 0; j--){tot[j] += dp[j];}}for (int i = 10000; i >= 0; i--){if (tot[i] == n){cout << i;return;}}
}
signed main()
{//cin.tie(0)->sync_with_stdio(false);int t = 1;//cin >> t;while (t--){solve();}return 0;
}

http://www.xdnf.cn/news/181459.html

相关文章:

  • Python中数据切片操作详解和代码示例
  • AI实战SEO关键词优化法
  • 【视频生成模型】通义万相Wan2.1模型本地部署和LoRA微调
  • 初中级前端面试全攻略:自我介绍模板、项目讲解套路与常见问答
  • LeetCode42_接雨水
  • 杭电oj(1010、1015、1241)题解
  • 【数据可视化-39】2009-2019年亚马逊50大畅销书数据集可视化分析
  • 迷你世界UGC3.0脚本Wiki世界模块管理接口 World
  • Mysql中隐式内连接和显式内连接的区别
  • (26)VTK C++开发示例 ---将点坐标写入PLY文件
  • linux:进程的替换
  • 大模型时代具身智能:从理论突破到产业落地的全链路解析
  • 自动伴随无人机说明文档
  • Netmiko 源码关键流程图
  • pytorch学习使用
  • 深入解析MyBatis-Plus中的lambdaUpdate与lambdaQuery
  • OpenCV 图形API(65)图像结构分析和形状描述符------拟合二维点集的直线函数 fitLine2D()
  • 文章记单词 | 第47篇(六级)
  • java map中的key区分大小写吗
  • ChatGPT与DeepSeek在科研论文撰写中的整体科研流程与案例解析
  • 【git】添加项目到已有gitee仓库
  • vue组件间通信
  • 蓝桥杯 9.生命之树
  • 【Multipath】dm软链接相关问题定位
  • 前端高频面试题day3
  • Python装饰器:函数增强的秘密武器
  • 使用ZXing开发安卓扫码功能
  • 【C++】C++11新特性(一)
  • 【前端】element表格X轴滚动优化拖拽滚动
  • 函数式编程之 Optional