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).
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).
Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
5 2 4 1
2
5 2 4 2
2
5 3 4 1
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)的更多相关文章
-
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 ...
-
Codeforces Round #274 Div.1 C Riding in a Lift --DP
题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...
-
Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
-
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 ...
-
Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)
题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...
-
Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
-
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 ...
-
Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...
-
Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)
题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...
随机推荐
-
iOS 9的升级后的问题处理
iOS 9变动影响SDK部分: 增加了bitCode编码格式,当SDK不支持bitCode时,用户集成时无法开启bitCode选项. 现象:用户集成SDK后无法编译通过,错误日志里包含了bitCode ...
-
hive 使用笔记(table format;lateral view)
1. create table 创建一张目标表,指定分隔符和存储格式: create table tmp_2 (resource_id bigint ,v int) ROW FORMAT DELIMI ...
-
jsp的标签
一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...
-
使用window2003安装邮件服务器最新实际操作记录
关于使用windows 2003自带的服务组件来安装简单的pop3 协议邮件服务器网上教程很多,可以搜索出来,就是安装IIS选中smtp和添加window是组件的应用程序服务器,这点这里不多说. 安装 ...
-
UML 类图基础知识记录
UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 依赖(Dependency): 关联(Association): 聚合(Aggregation): 合成(Composition): 泛化 ...
-
iOS 获取内存大小使用情况(进度条显示)
一.获取设备内存大小方法 //返回存储内存占用比例 - (NSString *)getFreeDiskspaceRate{ float totalSpace; .f; NSError *error = ...
-
Ubuntu 13.04 用户安装 gnome 3.8 桌面
昨天我试用了一把 Ubuntu gnome 13.04,如果你看了那一片文章:Ubuntu Gnome 13.04 体验截图.对 Ubuntu gnome 13.04 并不是采用的gnome 3.8 ...
-
VSCode 同步设置及扩展插件 实现设备上设置统一
准备工作:电脑上需安装VSCode,拥有一个github账户.实现同步的功能主要依赖于VSCode插件 "Settings Sync" Setting Sync 可同步包含的所有扩 ...
-
2017.11.19 C语言基础及流水灯实现
/* 从右往左*/ #include <reg52.h> sbit ADDR0 = P1^0; sbit ADDR1 = P1^1; sbit ADDR2 = P1^2; sbit ADD ...
-
Python列表、元组、字典和集合的区别
数据结构 是否可变 是否重复 是否有序 定义符号 列表(list) 可变 可重复 有序 [] 元组(tuple) 不可变 可重复 有序 () 字典(dictionary) 可变 可重复 无序 {key ...