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]);
}

Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp的更多相关文章

  1. Codeforces Round #274 Div.1 C Riding in a Lift --DP

    题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...

  2. Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)

    Imagine that you are in a building that has exactly n floors. You can move between the floors in a l ...

  3. Codeforces Round #274 (Div. 2) Riding in a Lift(DP 前缀和)

    Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. Codeforces Round #274 (Div. 2)

    A http://codeforces.com/contest/479/problem/A 枚举情况 #include<cstdio> #include<algorithm> ...

  5. Codeforces Round #274 (Div. 1) B. Long Jumps 数学

    B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...

  6. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  7. codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)

    题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #inclu ...

  8. Codeforces Round #274 (Div. 2)-C. Exams

    http://codeforces.com/contest/479/problem/C C. Exams time limit per test 1 second memory limit per t ...

  9. Codeforces Round #274 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...

随机推荐

  1. java中时间格式yyyyMMddHHmmss的大小写问题

    字母     日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996 ; 96 M 年中的月份 Month July ; Jul ; 07 w 年中的周数 Numb ...

  2. 【转】 IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  3. 回调函数的应用误区4(c/s OK版本回调小程序)

    VC++深入详解里面说得也挺好:回调函数的实现机制: 1)定义一个回调函数 2)“函数实现者”(回调函数所在的模块)在初始化的时候,将回调函数的函数指针注册给“调用者”. 3)当特定的事件或条件发生的 ...

  4. hdu 2594-Simpsons’ Hidden Talents(KMP)

    题意: 给你两个串a,b,求既是a的前缀又是b的后缀的最长子串的长度. 分析: 很自然的想到把两个串连接起来,根据KMP的性质求即可 #include <map> #include < ...

  5. PHP 系统命令函数

    function execute($cmd) { $res = ''; if ($cmd) { if(function_exists('system')) { @ob_start(); @system ...

  6. 3D 矩阵旋转

    如图,需要将点(向量) v(x, y, 0) 绕 z 轴旋转角度 θ,求旋转后的点(向量) v'(x', y', 0). 大概思路: 1. 将 v(x, y, 0) 分解, v(x, y, 0) = ...

  7. MorningSale 使用帮助

    待添加     http://121.37.42.173:8080/morningsale

  8. Unity 2D两种常用判断点击的方法

    1.Raycast法 原理相同于3D中得Raycast法,具体使用略有区别. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorl ...

  9. web服务器分析与设计(四)

    上篇已经开始了系统内部的分析,并且得到一些分析对象.在整个动作场景中,我们得到了一些粗略的对象.有必要对对象进行分析,合并,再抽象. 实质是职责的合理分配,使得系统合乎功能性,同时得到最大的可扩展,可 ...

  10. public private protected和默认的区别(转自百度)

    public private protected和默认的区别 Java中对类以及类中的成员变量和成员方法通过访问控制符(access specifier)进行区分控制.刚学Java语言的同学可能对pu ...