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 nabk (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ na ≠ b).

Output

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

Sample test(s)
input
5 2 4 1
output
2
input
5 2 4 2
output
2
input
5 3 4 1
output
0

题意:做电梯。刚開始的时候你在a层,不能到b层,每次你到新的地方的y,必须满足|x-y|<|x-b|,求坐k次有多少种可能

思路:比較easy想到的是dp[i][j]表示第i次到了j层的可能。分情况讨论。比如:当a<b的时候。下一次的层数i是不能超过j+(b-j-1)/2的,然后每次预先处理出前j层的可能。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int mod = 1000000007;
const int maxn = 5005; int n, a, b, k, dp[maxn][maxn];
int sum[maxn]; int main() {
scanf("%d%d%d%d", &n, &a, &b, &k);
memset(dp, 0, sizeof(dp));
if (a < b) {
dp[0][a] = 1;
for (int j = 1; j < b; j++)
sum[j] = sum[j-1] + dp[0][j];
for (int i = 1; i <= k; i++) {
for (int j = 1; j < b; j++)
dp[i][j] = (sum[(b-j-1)/2+j] - dp[i-1][j] + mod) % mod;
sum[0] = 0;
for (int j = 1; j < b; j++)
sum[j] = (sum[j-1] + dp[i][j]) % mod;
}
printf("%d\n", sum[b-1]);
}
else {
dp[0][a] = 1;
for (int j = n; j >= b+1; j--)
sum[j] = sum[j+1] + dp[0][j];
for (int i = 1; i <= k; i++) {
for (int j = b+1; j <= n; j++)
dp[i][j] = (sum[j-(j-b-1)/2] - dp[i-1][j] + mod) % mod;
sum[0] = 0;
for (int j = n; j >= b+1; j--)
sum[j] = (sum[j+1] + dp[i][j]) % mod;
}
printf("%d\n", sum[b+1]);
}
return 0;
}

Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)的更多相关文章

  1. 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/pr ...

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

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

  3. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  4. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  6. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  7. Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)

    链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a ...

  8. Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

    题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...

  9. Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)

    题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...

随机推荐

  1. CSS和CSS3中的伪元素和伪类(总结)

    好多人伪类和伪元素分清楚,其实就是一句话,“伪类的效果可以通过添加一个实际的类来达到,而伪元素的效果则需要通过添加一个实际的元素才能达到”. CSS中伪类包括: :first-child :lang ...

  2. linux性能分析命令top

    发布时间: 2013-12-14浏览次数:154分类: 服务器 top是linux最常用的性能分析工具了,它是个交互式工具,提供系统的整体性能,如正在执行的进程信息包括进程ID,内存占用率,CPU占用 ...

  3. Linux文件和目录操作管理命令

    1.pwd:显示工作目录路径 -p:显示实际物理路径 -l:显示链接路径 2.cd:更改工作目录路径 cd:进入用户主目录 cd~:进入用户主目录 cd-:返回进入此目录之前所在的目录 cd..:返回 ...

  4. java操作csv文件之javacsv.jar应用

    csv文件是分隔文件,如果使用java的io流来写,比较麻烦,这里为大家提供一个javacsv的jar包,这个很方便操作csv文件. 下载地址:https://pan.baidu.com/s/1i46 ...

  5. Using Notepad++ to Execute Oracle SQL

    原文链接:http://www.toadworld.com/products/toad-for-oracle/b/weblog/archive/2013/08/21/using-notepad-to- ...

  6. Swift - 40 - 枚举更加灵活的使用方式

    //: Playground - noun: a place where people can play import UIKit /* 这里的枚举没有给它的成员默认值, 而是给它绑定了一个类型, 之 ...

  7. PHP XML DOM

    PHP XML DOM 内建的 DOM 解析器使在 PHP 中处理 XML 文档成为可能. DOM 是什么? W3C DOM 提供了针对 HTML 和 XML 文档的标准对象集,以及用于访问和操作这些 ...

  8. 持续集成环境(Gitlab+jenkins+shell)

    一.搭建gitlab ps:不是这方面的专家,主要还是一键式安装为主. 1.进入官网:https://about.gitlab.com/gitlab-com/ 2.选择自己的操作系统:我这边选择的ub ...

  9. (四 )Knockout - ViewModel 的使用3 - 对象属性变化的实时更新

    ko.observableArray()就可以自动检测属性,其实他只是监控对象,而不是对象中的属性 使用ko.observable()进行处理 DEMO1 实时更新属性 //定义user数据对象 va ...

  10. redis配置文件redis.conf详细说明

    # By default Redis does not run as a daemon. Use 'yes' if you need it.# Note that Redis will write a ...