当前位置:网站首页>【带限制的完全背包】Educational Codeforces Round 133 (Rated for Div. 2) D. Chip Move
【带限制的完全背包】Educational Codeforces Round 133 (Rated for Div. 2) D. Chip Move
2022-08-07 17:15:00 【solego】
题意:
给定 n n n 和 k k k,初始步长为 k k k ,每次可以走 k k k 的倍数次步,下一次走 ( k + 1 ) (k+1) (k+1)的倍数次步,以此类推,问从 0 0 0 开始到达 [ 1 , n ] [1,n] [1,n] 每个点的方案数。
题解:
当每次只走一倍的步数时, ( s t e p + k ) × ( s t e p − k + 1 ) 2 ≤ n \frac{(step+k)\times (step-k+1)}{2}\leq n 2(step+k)×(step−k+1)≤n,所以最多只会走 O ( n ) O(\sqrt{n}) O(n)次。
问题转换为带限制的完全背包问题,走第 i i i 次前,必须已经走了 1 1 1 到 i − 1 i-1 i−1次
f [ i ] [ j ] f[i][j] f[i][j] 表示走了 i i i次,第 i i i 次走完后到达点 j j j 的方案数
第 i i i 次走的步数为 ( k + i − 1 ) (k+i-1) (k+i−1) 的倍数
不带限制的完全背包的模板为:
for (int i = 1; i <= n; ++i) f[i] = 0;
f[0] = 1;
for (int step = k; (start = (step + k) * (step - k + 1) / 2) <= n; ++step)
for (int j = 0; j <= n; ++j)
if (j >= step) f[i][j] += f[i - 1][j - step];
但是本题的限制为:走第 i i i 次前,必须已经走了 1 1 1 到 i − 1 i-1 i−1次
每次需要强制走一下第 i − 1 i-1 i−1 次,如下:
for (int i = 1; i <= n; ++i) f[0][i] = 0;
f[0][0] = 1;
for (int step = k; (step + k) * (step - k + 1) / 2 <= n; ++step) {
for (int j = 0; j <= n; ++j)
if (j >= step) f[i][j] = f[i - 1][j - step];
else f[i][j] = 0;
for (int j = 0; j <= n; ++j)
if (j >= step) f[i][j] += f[i - 1][j - step];
for (int j = 1; j <= n; ++j) ans[j] += f[j];
}
滚动数组优化一下:
for (int i = 1; i <= n; ++i) f[i] = 0;
f[0] = 1;
for (int step = k; (step + k) * (step - k + 1) / 2 <= n; ++step) {
for (int j = n; j >= 0; --j)
if (j >= step) f[j] = f[j - step];
else f[j] = 0;
for (int j = 0; j <= n; ++j)
if (j >= step) f[j] += f[j - step];
for (int j = 1; j <= n; ++j) ans[j] += f[j];
}
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
const int mod = 998244353;
int dp[2][N];
int f[N];
int ans[N];
int n, k;
void add(int& a, int b) {
a += b;
if (a >= mod) a -= mod;
}
void solve() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) f[i] = 0;
f[0] = 1;
for (int step = k, start; (start = (step + k) * (step - k + 1) / 2) <= n; ++step) {
for (int j = n; j >= 0; --j)
if (j >= start) f[j] = f[j - step];
else f[j] = 0;
for (int j = start; j <= n; ++j)
if (j >= start) add(f[j], f[j - step]);
for (int j = 1; j <= n; ++j) add(ans[j], f[j]);
}
for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], " \n"[i == n]);
}
int main()
{
int T = 1;
// scanf("%d", &T);
for (int i = 1; i <= T; ++i) {
solve();
}
return 0;
}
总结:
避免跳跃性思考,从朴素的二维状态构建好转移方程后,再根据实际优化空间和时间
边栏推荐
- 客户流失?来看看大厂如何基于spark+机器学习构建千万数据规模上的用户留存模型
- MySQL面试必备(四)锁篇
- Error:Connection refused: connect
- qq聊天截图怎么截长图 使用qq长截图的图文步骤
- 【论文理解】Batch Normalization论文中关于BN背景和减少内部协变量偏移的解读(论文第1、2节)
- 数字货币现货合约交易所系统开发技术分析
- 笔记本接显卡怎么操作 笔记本外接显卡详细教程
- 设置开机项的步骤 怎么设置开机启动项
- Which software is easy to use for reinstalling the system, which is the best software for reinstalling the computer system
- 单片机常识篇
猜你喜欢

VScode的代码截图插件CodeSnap

VScode's code screenshot plugin CodeSnap

电脑看视频卡怎么解决 电脑看视频一卡一卡的解决教程

分布式一致性协议 之 两阶段提交协议(2PC)

ELK日志平台搭建(一)

电脑上看电视没有声音怎么办 台式电脑看视频没声音怎么办

Chrome scroll bar style modification how to operate Chrome browser scroll bar style customization method

qq聊天截图怎么截长图 使用qq长截图的图文步骤

How to view web browsing history How to check history web browsing history

Win7系统升级失败提示错误代码0X80070643如何解决
随机推荐
How to solve the problem that the U disk cannot be detected in win10_How to solve the problem that the U disk cannot be recognized by the win10 system
Chrome scroll bar style modification how to operate Chrome browser scroll bar style customization method
win7查询物理地址方法 win7怎么查物理地址
CNAS实验室运作过程示意图
How to handle the wireless network card of the laptop How to handle the wireless network card of the laptop
win7系统电脑更换锁屏壁纸的方法
Which software is easy to use for reinstalling the system, which is the best software for reinstalling the computer system
How to clean up computer C drive Win7 clean up computer C drive junk files method introduction
桌面上的文件夹不见了怎么办 桌面图标不见了怎么办
鼠标单击偶尔变双击了怎么办_电脑鼠标单击变双击了怎么设置
EL 表达式
Epoller
how to set up a computer wireless network how to set up a wireless network connection
一文看懂Filter过滤器
重装系统什么软件好用 电脑系统重装哪个最好用
R语言拟合ARIMA模型并使用拟合模型进行预测推理、使用autoplot函数可视化ARIMA模型预测结果(返回的对象为ggplot2对象)
【面试备战章】计网(一)基础篇——1 TCP/IP网络模型
十、MFC控件(二)
CNAS认可准则之纠正、纠正措施和预防措施的区别
FutureTask源码深度剖析