首页 > 代码库 > CodeForces 274E. Riding in a LiftDp

CodeForces 274E. Riding in a LiftDp

题意:从a 开始不能到达b,要坐k次电梯的满足条件 :|x - y| < |x - b|. x是当前位置,y是目标位置 的方案数。

每次转移处理个前缀和,然后每次满足条件的和的范围是 :1到 (y+b-1) /2 

#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include <math.h>using namespace std;typedef long long LL;const int maxn  = 5555;int dp[maxn][maxn];int sum[maxn][maxn];const int mod = 1000000000+7;int main(){    int n, a, b, k;    cin >> n >> a >> b >> k;    if (a > b) a = n + 1 - a, b = n + 1 - b;    dp[0][a] = 1;    for (int i = 1; i <= k; i++){        for (int j = 1; j < b; j++)            sum[i-1][j] = sum[i-1][j - 1] + dp[i-1][j],sum[i-1][j]%=mod;        for (int j = 1; j < b; j++){            int r= (j+b-1) / 2;            dp[i][j] = sum[i - 1][r] - sum[i - 1][0] - dp[i - 1][j];            dp[i][j] = (dp[i][j]+mod) %mod;        }    }    int ans = 0;    for (int i = 1; i < b; i++)        ans += dp[k][i],ans%=mod;    printf("%d\n", ans);    return 0;}

 

CodeForces 274E. Riding in a LiftDp