当前位置:网站首页>E - Distance Sequence (prefix and optimized dp
E - Distance Sequence (prefix and optimized dp
2022-08-05 00:22:00 【__Rain】
E - Distance Sequence
思路:
d p [ i ] [ j ] dp[i][j] dp[i][j] 表示考虑前 i i i 个数,最后一个数为 j j j 时的方案数
显然 d p [ i ] [ j ] + = d p [ i − 1 ] [ l ] dp[i][j]+=dp[i-1][l] dp[i][j]+=dp[i−1][l], l l l 为前 i − 1 i-1 i−1 个数中以 l l l End and with j j j In the case of the joint method
j − l ≥ k j-l\geq k j−l≥k,即 1 ≤ l ≤ j − k 1\leq l \leq j-k 1≤l≤j−k
l − j ≥ k l-j\geq k l−j≥k,即 j + k ≤ l ≤ m j+k \leq l \leq m j+k≤l≤m
Obviously a prefix and optimization can be maintained
(Note that special judgment is required K = 0 K=0 K=0 的情况
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
ll dp[1009][5009];
void work()
{
int k;
cin >> n >> m >> k;
if(k == 0){
ll ans = 1;
for(int i = 1; i <= n; ++i)
ans = ans * m % mod;
cout << ans;
return;
}
vector <ll> sum(m + 1, 0);
for(int i = 1; i <= m; ++i) {
dp[1][i] = 1;
sum[i] = sum[i-1] + dp[1][i];
}
for(int i = 2; i <= n; ++i){
vector <ll> now_sum(m + 1, 0);
for(int j = 1; j <= m; ++j){
if(j - k >= 1)
(dp[i][j] += sum[j - k]) %= mod;
//for(int l = 1; l <= j - k; ++l)
// (dp[i][j] += dp[i-1][l]) %= mod;
if(j + k <= m)
(dp[i][j] += (sum[m] - sum[j+k-1] + mod) % mod) %= mod;
//for(int l = j + k; l <= m; ++l)
// (dp[i][j] += dp[i-1][l]) %= mod;
}
for(int j = 1; j <= m; ++j) now_sum[j] = (now_sum[j-1] + dp[i][j]) % mod;
sum = now_sum;
}
cout << sum[m];
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}
改进 d p dp dp The process does not need to be judged individually,But also think about it k = = 0 k==0 k==0 the situation,Then modify the transfer equation
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m, k;
ll dp[1009][5009];
void work()
{
cin >> n >> m >> k;
vector <ll> sum(m + 1, 0);
for(int i = 1; i <= m; ++i) {
dp[1][i] = 1;
sum[i] = sum[i-1] + dp[1][i];
}
for(int i = 2; i <= n; ++i){
vector <ll> now_sum(m + 1, 0);
for(int j = 1; j <= m; ++j){
int l = max(1ll, j - k + 1), r = min(m, j + k - 1);
if(k){
dp[i][j] = (sum[m] - (sum[r] - sum[l-1]) + mod) % mod;
}
else {
dp[i][j] = sum[m];
}
}
for(int j = 1; j <= m; ++j) now_sum[j] = (now_sum[j-1] + dp[i][j]) % mod;
sum = now_sum;
}
cout << sum[m];
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}
想到 k = 0 k=0 k=0 Then change the transfer equation,I can't do it for a chicken like me,Hence the following writing,It can be completely ignored K K K 的值了,Just make sure every transfer is legal(也就是 r > = l − 1 r>=l-1 r>=l−1)
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m, k;
ll dp[1009][5009];
// 考虑前i个数,最后一个数为j
void work()
{
cin >> n >> m >> k;
vector <ll> sum(m + 1, 0);
for(int i = 1; i <= m; ++i) {
// 单独一个数1-m都可以
dp[1][i] = 1;
sum[i] = sum[i-1] + dp[1][i];
}
for(int i = 2; i <= n; ++i){
vector <ll> now_sum(m + 1, 0);
for(int j = 1; j <= m; ++j){
int l = max(1ll, j - k + 1), r = min(m, j + k - 1);// 不合法的区间
dp[i][j] = sum[m];
if(r >= l - 1) {
// If the illegal interval exists, it needs to be deleted
dp[i][j] = (dp[i][j] - (sum[r] - sum[l-1]) + mod) % mod;
}
}
for(int j = 1; j <= m; ++j) now_sum[j] = (now_sum[j-1] + dp[i][j]) % mod;
sum = now_sum;
}
cout << sum[m];
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}
边栏推荐
- [LeetCode] Summary of Matrix Simulation Related Topics
- 2022杭电多校第三场 K题 Taxi
- leetcode经典例题——单词拆分
- "No title"
- .net (C#) get year month day between two dates
- About I double-checked and reviewed the About staff page, returning an industry question
- matlab中rcosdesign函数升余弦滚降成型滤波器
- "WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
- 导入JankStats检测卡帧库遇到问题记录
- E - Many Operations (bitwise consideration + dp thought to record the result after the operation
猜你喜欢

【LeetCode】Summary of Two Pointer Problems

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

Will domestic websites use Hong Kong servers be blocked?

leetcode:266. 回文全排列

进程间通信和线程间通信

Senior game modelers tell newbies, what are the necessary software for game scene modelers?

子连接中的参数传递

刘润直播预告 | 顶级高手,如何创造财富

图解 Canvas 入门

Three tips for you to successfully get started with 3D modeling
随机推荐
论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
子连接中的参数传递
MAUI Blazor 权限经验分享 (定位,使用相机)
#yyds dry goods inventory #Switching equipment serious packet loss troubleshooting
Huggingface入门篇 II (QA)
.net (C#) get year month day between two dates
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
关于使用read table 语句
软件测试面试题:做好测试计划的关键是什么?
tiup uninstall
matlab中rcosdesign函数升余弦滚降成型滤波器
MAUI Blazor 权限经验分享 (定位,使用相机)
2022 Hangzhou Electric Multi-School Training Session 3 1009 Package Delivery
克服项目管理中恐惧心理
Chinese and Japanese color style
2022牛客多校训练第二场 H题 Take the Elevator
电子行业MES管理系统的主要功能与用途
【LeetCode】Summary of Two Pointer Problems
MongoDB permission verification is turned on and mongoose database configuration
gorm joint table query - actual combat