计蒜客T31434 广场车神(二维前缀和优化dp)
程序员文章站
2022-03-26 20:17:18
题目链接Reference:https://www.cnblogs.com/dilthey/p/9757781.html首先容易想到的常规dp是,初始化dp(i,j)=0dp(i,j)=0dp(i,j)=0,对于当前下标(i,j)(i,j)(i,j)为右上角的一个边长为k+1k+1k+1的正方形内:dp(i,j)=∑x=i−ki∑y=j−kjdp(x,y)dp(i,j)= \sum_{x=i-k}^{i} \sum_{y=j-k}^{j}dp(x,y)dp(i,j)=∑x=i−ki∑y=j−kj...
Reference:https://www.cnblogs.com/dilthey/p/9757781.html
首先容易想到的常规dp是,初始化,对于当前下标为右上角的一个边长为的正方形内:
但是这样的时间复杂度为,使用二维前缀和优化
设最后dp方程的二维前缀和,因为当前状态只能从一个的正方形内的状态转移,如果使用二维前缀和计算这一方形区域,设下边界为,左边界为,状态转移方程为:
,画个图很容易看出来,然后根据二维前缀和的定义:
那么最后的答案就是:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const double dinf=1e300;
const ll INF=1e18;
const int Mod=998244353;
const int maxn=2e5+10;
ll sum[2020][2020];
int main(){
freopen("racing.in","r",stdin);
freopen("racing.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,m,k;
cin>>n>>m>>k;
sum[1][1]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(i==1 && j==1) continue;
int l=max(0,i-k-1),d=max(0,j-k-1);
sum[i][j]=(2*(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1])%Mod-(sum[l][j]+sum[i][d]-sum[l][d])%Mod+Mod)%Mod;
}
ll ans=(sum[n][m]-(sum[n-1][m]+sum[n][m-1]-sum[n-1][m-1])%Mod+Mod)%Mod;
cout<<ans<<endl;
return 0;
}
本文地址:https://blog.csdn.net/qq_44691917/article/details/107441455
上一篇: vue-cli3安装