Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

C. Riding in a Lift

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/480/problem/C

Description

Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.

Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y ≠ x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.

Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).

Input

The first line of the input contains four space-separated integers n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).

Output

Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).

Sample Input

5 2 4 1

Sample Output

2

HINT

题意

有n层楼,你一开始在a层楼,实验室在b层楼,然后你可以瞎走k次

每次只要满足|now-next|<|now-b|就可以从now走到next去

然后问你一共有多少种走法

题解:

最简单就是dp[i][j]表示我现在在第i层,瞎走了j次的方案数,但是这个转移是n^3的

我们得优化一下

每次我们可以看到,从上一个状态转移过来的是一个区间,所以大概用一个线段树优化成n^2logn或者用前缀和优化成n^2的就行了

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define maxn 5005
#define mod 1000000007
int dp[maxn][maxn];
int sum[maxn];
int main()
{
int n,a,b,k;
scanf("%d%d%d%d",&n,&a,&b,&k);
dp[a][]=;
for(int i=;i<=n;i++)
sum[i]=dp[i][]+sum[i-];
for(int i=;i<=k;i++)
{
for(int j=;j<=n;j++)
{
if(j==b)continue;
int L,R;
if(j<b)
{
L = ,R = (j+b)/;
if(R-j==b-R)R--;
}
else
{
L = (j+b)/+(j+b)%;R=n;
if(j-L==L-b)L++;
}
L=max(,L);R=min(n,R);
dp[j][i]=((sum[R]-sum[L-]+mod)%mod-dp[j][i-]+mod)%mod;
}
for(int j=;j<=n;j++)
{
sum[j]=dp[j][i]+sum[j-];
while(sum[j]>=mod)sum[j]-=mod;
}
}
printf("%d\n",sum[n]);
}
上一篇:[转]说说C#的async和await


下一篇:查看mysql语句运行时间的2种方法