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. Asp.Net Api2 过滤器的使用

    1.注意: apiController控制器 对应的过滤器System.Web.Http.Filters.ActionFilterAttribute的过滤器 MVC的Controller控制器 对应的 ...

  2. 制作ado开发辅助工具类SqlHelper

    public static class SqlHelper { //通过配置文件获取连接字符创 private static readonly string constr = Configuratio ...

  3. pt-query-digest 安装及使用

    打个草稿 介绍:pt-query-digest 可用于mysql的慢查询的日志分析,分析统计出每种慢查询的基本信息,如响应时间.最大执行时间.最小执行时间.执行时间的中位数等.(当然不只是这个功能) ...

  4. JavaScript--基本包装类型+Math对象

    1. 基本包装类型 1)为了便于操作基本类型值,ECMAScript提供了3个特殊的引用类Boolean, Number, String       每当读取一个基本类型值的时候,后台就会创建一个对应 ...

  5. 高放的c++学习笔记之函数基础

    局部变量 函数里面的局部变量有普通的局部变量和局部静态变量两种. 普通局部变量变量就是正常定义在函数体内部的变量,如果返回局部变量的地址,以便于函数调用结束后还继续访问此变量的话,编译器会报warni ...

  6. js 浏览器版本检测

    整理了一下浏览器检测的js脚本 分享给大家 浏览器检测一般都是在网页打开的时候执行 使用js的闭包来实现页面加载以后执行的脚本 (function(){ //页面加载后执行的脚本 })() ; 检测浏 ...

  7. view ondraw

    窗口发生重绘时会被应用程序的窗口框架给调用 要使输出的东西始终能在窗口中看到 就可以使用该函数  窗口从到有的时候就会产生WM_PAINT消息,让窗口发生重绘 这是程序就会执行到ONDRAW函数处 所 ...

  8. nodejs调试

    1.通过debug命令进行调试 node debug app.js 运行的结果: 在debug状态下输入"repl"命令可以评估变量和表达式的值 按下'CTRL+C'可以退出rep ...

  9. 初探react

    知道这个框架有一段时间了,可是项目中没有用到,也懒得整理,最近两天比较清闲,又想起了它.好了,废话不多说了,都是干货. react是个什么框架? 为什么用react? react 的原理 react有 ...

  10. node学习笔记-搭建node环境

    最近项目要用到node,利用空闲整理做下笔记 第一步  安装node,方式比较多,最为直接的是直接去官网     可直接从官网下载安装http://nodejs.cn/download/ 根据自己情况 ...