当前位置: 代码迷 >> 综合 >> CodeForces 479E Riding in a Lift (优化DP,打标记+前缀和)
  详细解决方案

CodeForces 479E Riding in a Lift (优化DP,打标记+前缀和)

热度:90   发布时间:2023-11-15 11:07:09.0

主要是往下更新下一层DP的时候要用到前缀和的思想打标记.

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=5e3+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);
}
int n,a,b,k;
int dp[maxn][maxn];
int main(){cin>>n>>a>>b>>k;dp[0][a]=1;rep(i,0,k){rep(j,1,n+1) if(dp[i][j]){int dis=abs(j-b);///if(dis==1) continue;(dp[i+1][max(1,j-dis+1)]+=dp[i][j])%=mod;(dp[i+1][min(n+1,j+dis)]-=dp[i][j])%=mod;}dp[i+1][0]=0;rep(j,1,n+1) (dp[i+1][j]+=dp[i+1][j-1])%=mod;rep(j,1,n+1) dp[i+1][j]=((dp[i+1][j]-dp[i][j])%mod+mod)%mod;}int ans=0;rep(i,1,n+1) (ans+=dp[k][i])%=mod;cout<<ans;return 0;
}